1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2001-2002, OpenBeOS 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a 5 // copy of this software and associated documentation files (the "Software"), 6 // to deal in the Software without restriction, including without limitation 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 // and/or sell copies of the Software, and to permit persons to whom the 9 // Software is furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 // DEALINGS IN THE SOFTWARE. 21 // 22 // File Name: Clipboard.cpp 23 // Author: Gabe Yoder (gyoder@stny.rr.com) 24 // Description: BClipboard provides an interface to a system-wide clipboard 25 // storage area. 26 //------------------------------------------------------------------------------ 27 28 // Standard Includes ----------------------------------------------------------- 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 33 // System Includes ------------------------------------------------------------- 34 #include <Clipboard.h> 35 #include <RegistrarDefs.h> 36 #include <Application.h> 37 38 // Project Includes ------------------------------------------------------------ 39 40 // Local Includes -------------------------------------------------------------- 41 42 // Local Defines --------------------------------------------------------------- 43 44 // Globals --------------------------------------------------------------------- 45 46 47 //------------------------------------------------------------------------------ 48 BClipboard::BClipboard(const char *name, bool transient = false) 49 { 50 if ( name ) 51 fName = strdup(name); 52 else 53 fName = strdup("system"); 54 fData = new BMessage(); 55 fCount = 0; 56 fSystemCount = 0; 57 58 BMessage message(B_REG_GET_CLIPBOARD_MESSENGER), reply; 59 if ( (_send_to_roster_(&message, &reply, false) == B_OK) && 60 (reply.what == B_REG_SUCCESS) && 61 (reply.FindMessenger("messenger",&fClipHandler) == B_OK) ) 62 { 63 BMessage handlerMessage(B_REG_ADD_CLIPBOARD), handlerReply; 64 int32 result; 65 if ( (handlerMessage.AddString("name",fName) == B_OK) && 66 (fClipHandler.SendMessage(&handlerMessage, &handlerReply) == B_OK) ) 67 handlerReply.FindInt32("result",&result); 68 } 69 } 70 //------------------------------------------------------------------------------ 71 BClipboard::~BClipboard() 72 { 73 free(fName); 74 delete fData; 75 } 76 //------------------------------------------------------------------------------ 77 const char* BClipboard::Name() const 78 { 79 return (const char*)fName; 80 } 81 //------------------------------------------------------------------------------ 82 uint32 BClipboard::LocalCount() const 83 { 84 /* fSystemCount contains the total number of writes to the clipboard. 85 fCount contains the number of writes to the clipboard done by this 86 BClipboard. 87 */ 88 return fSystemCount; 89 } 90 //------------------------------------------------------------------------------ 91 uint32 BClipboard::SystemCount() const 92 { 93 int32 val; 94 BMessage message(B_REG_GET_CLIPBOARD_COUNT), reply; 95 if ( (message.AddString("name",fName) == B_OK) && 96 (fClipHandler.SendMessage(&message, &reply) == B_OK) && 97 (reply.FindInt32("count",&val) == B_OK) ) 98 return (uint32)val; 99 100 return 0; 101 } 102 //------------------------------------------------------------------------------ 103 status_t BClipboard::StartWatching(BMessenger target) 104 { 105 BMessage message(B_REG_CLIPBOARD_START_WATCHING), reply; 106 if ( (message.AddString("name",fName) == B_OK) && 107 (message.AddMessenger("target", target ) == B_OK) && 108 (fClipHandler.SendMessage(&message, &reply) == B_OK) ) 109 { 110 int32 result; 111 reply.FindInt32("result",&result); 112 return result; 113 } 114 return B_ERROR; 115 } 116 //------------------------------------------------------------------------------ 117 status_t BClipboard::StopWatching(BMessenger target) 118 { 119 BMessage message(B_REG_CLIPBOARD_STOP_WATCHING), reply; 120 if ( (message.AddString("name",fName) == B_OK) && 121 (message.AddMessenger("target", target ) == B_OK) && 122 (fClipHandler.SendMessage(&message, &reply) == B_OK) ) 123 { 124 int32 result; 125 reply.FindInt32("result",&result); 126 return result; 127 } 128 return B_ERROR; 129 } 130 //------------------------------------------------------------------------------ 131 bool BClipboard::Lock() 132 { 133 /* Will this work correctly if clipboard is deleted while still waiting on 134 fLock.Lock() ? */ 135 bool retVal; 136 retVal = fLock.Lock(); 137 if ( retVal && 138 (DownloadFromSystem() != B_OK) ) 139 { 140 retVal = false; 141 fLock.Unlock(); 142 } 143 144 return retVal; 145 } 146 //------------------------------------------------------------------------------ 147 void BClipboard::Unlock() 148 { 149 fLock.Unlock(); 150 } 151 //------------------------------------------------------------------------------ 152 bool BClipboard::IsLocked() const 153 { 154 return fLock.IsLocked(); 155 } 156 //------------------------------------------------------------------------------ 157 status_t BClipboard::Clear() 158 { 159 if ( AssertLocked() && 160 (fData->MakeEmpty() == B_OK) ) 161 return B_OK; 162 return B_ERROR; 163 } 164 //------------------------------------------------------------------------------ 165 status_t BClipboard::Commit() 166 { 167 if ( AssertLocked() && 168 (UploadToSystem() == B_OK) ) 169 return B_OK; 170 return B_ERROR; 171 } 172 //------------------------------------------------------------------------------ 173 status_t BClipboard::Revert() 174 { 175 if ( AssertLocked() && 176 (fData->MakeEmpty() == B_OK) && 177 (DownloadFromSystem() == B_OK) ) 178 return B_OK; 179 return B_ERROR; 180 } 181 //------------------------------------------------------------------------------ 182 BMessenger BClipboard::DataSource() const 183 { 184 return fDataSource; 185 } 186 //------------------------------------------------------------------------------ 187 BMessage* BClipboard::Data() const 188 { 189 if ( IsLocked() ) 190 return fData; 191 return NULL; 192 } 193 //------------------------------------------------------------------------------ 194 BClipboard::BClipboard(const BClipboard &) 195 { 196 /* This is private, and I don't use it, so I'm not going to implement it */ 197 } 198 //------------------------------------------------------------------------------ 199 BClipboard & BClipboard::operator=(const BClipboard &) 200 { 201 /* This is private, and I don't use it, so I'm not going to implement it */ 202 } 203 //------------------------------------------------------------------------------ 204 void BClipboard::_ReservedClipboard1() 205 { 206 } 207 //------------------------------------------------------------------------------ 208 void BClipboard::_ReservedClipboard2() 209 { 210 } 211 //------------------------------------------------------------------------------ 212 void BClipboard::_ReservedClipboard3() 213 { 214 } 215 //------------------------------------------------------------------------------ 216 bool BClipboard::AssertLocked() const 217 { 218 /* How should this be different from IsLocked() */ 219 return fLock.IsLocked(); 220 } 221 //------------------------------------------------------------------------------ 222 status_t BClipboard::DownloadFromSystem(bool force=false) 223 { 224 /* What does force mean? */ 225 BMessage message(B_REG_DOWNLOAD_CLIPBOARD), reply; 226 if ( (message.AddString("name",fName) == B_OK) && 227 (fClipHandler.SendMessage(&message, &reply) == B_OK) && 228 (reply.FindMessage("data",fData) == B_OK) && 229 (reply.FindMessenger("data source",&fDataSource) == B_OK) && 230 (reply.FindInt32("count",(int32 *)(&fSystemCount)) == B_OK) ) 231 return B_OK; 232 return B_ERROR; 233 } 234 //------------------------------------------------------------------------------ 235 status_t BClipboard::UploadToSystem() 236 { 237 BMessage message(B_REG_UPLOAD_CLIPBOARD), reply; 238 if ( (message.AddString("name",fName) == B_OK) && 239 (message.AddMessage("data",fData) == B_OK) && 240 (message.AddMessenger("data source", be_app_messenger ) == B_OK) && 241 (fClipHandler.SendMessage(&message, &reply) == B_OK) && 242 (reply.FindInt32("count",(int32 *)(&fSystemCount)) == B_OK) ) 243 { 244 fCount++; 245 return B_OK; 246 } 247 return B_ERROR; 248 } 249 //------------------------------------------------------------------------------ 250 251 252