1
/*********************************************************
2
* Copyright (C) 2010 VMware, Inc. All rights reserved.
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License as published
6
* by the Free Software Foundation version 2.1 and no later version.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
11
* License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program; if not, write to the Free Software Foundation, Inc.,
15
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17
*********************************************************/
20
* @guestCopyPasteSrc.cc --
22
* Implementation of common layer GuestCopyPasteSrc object for guest.
25
#include "guestCopyPaste.hh"
28
#include "dndClipboard.h"
31
#include "cpNameUtil.h"
40
* @param[in] mgr guest CP manager
43
GuestCopyPasteSrc::GuestCopyPasteSrc(GuestCopyPasteMgr *mgr)
47
mMgr->GetRpc()->getFilesDoneChanged.connect(
48
sigc::mem_fun(this, &GuestCopyPasteSrc::OnRpcGetFilesDone));
49
CPClipboard_Init(&mClipboard);
57
GuestCopyPasteSrc::~GuestCopyPasteSrc(void)
60
CPClipboard_Destroy(&mClipboard);
61
/* Reset current session id after finished. */
62
mMgr->SetSessionId(0);
67
* Got valid clipboard data from host. Emit srcRecvClipChanged signal if state
70
* @param[in] isActive active or passive CopyPaste.
71
* @param[in] clip cross-platform clipboard data.
75
GuestCopyPasteSrc::OnRpcRecvClip(bool isActive,
76
const CPClipboard *clip)
81
Debug("%s: state is %d\n", __FUNCTION__, mMgr->GetState());
83
CPClipboard_Clear(&mClipboard);
84
CPClipboard_Copy(&mClipboard, clip);
86
mMgr->srcRecvClipChanged.emit(clip);
91
* UI is asking for files. Send requestFiles cmd to controller.
93
* @param[in] dir staging directory in local format.
95
* @return The staging directory if succeed, otherwise empty string.
99
GuestCopyPasteSrc::UIRequestFiles(const std::string &dir)
102
char cpName[FILE_MAXPATH];
105
if (mMgr->GetState() != GUEST_CP_READY) {
106
/* Reset DnD for any wrong state. */
107
Debug("%s: Bad state: %d\n", __FUNCTION__, mMgr->GetState());
111
/* Setup staging directory. */
112
destDir = SetupDestDir(dir);
113
if (destDir.empty()) {
117
/* Convert staging name to CP format. */
118
cpNameSize = CPNameUtil_ConvertToRoot(destDir.c_str(),
121
if (cpNameSize < 0) {
122
Debug("%s: Error, could not convert to CPName.\n", __FUNCTION__);
126
if (!mMgr->GetRpc()->RequestFiles(mMgr->GetSessionId(),
127
(const uint8 *)cpName,
132
mStagingDir = destDir;
133
mMgr->SetState(GUEST_CP_HG_FILE_COPYING);
134
Debug("%s: state changed to GUEST_CP_HG_FILE_COPYING\n", __FUNCTION__);
139
mMgr->ResetCopyPaste();
145
* The file transfer is finished. Emit getFilesDoneChanged signal and reset
148
* @param[in] sessionId active DnD session id
149
* @param[in] success if the file transfer is successful or not
150
* @param[in] stagingDirCP staging dir name in cross-platform format
151
* @param[in] sz the staging dir name size
155
GuestCopyPasteSrc::OnRpcGetFilesDone(uint32 sessionId,
157
const uint8 *stagingDirCP,
160
if (!success && !mStagingDir.empty()) {
161
/* Delete all files if host canceled the file transfer. */
162
DnD_DeleteStagingFiles(mStagingDir.c_str(), FALSE);
165
/* UI should remove block with this signal. */
166
mMgr->getFilesDoneChanged.emit(success);
167
mMgr->SetState(GUEST_CP_READY);
168
Debug("%s: state changed to READY\n", __FUNCTION__);
173
* Creates a directory for file transfer. If the destination dir is provided,
174
* we will attempt to copy files to that directory.
176
* @param[in] destDir the preferred destination directory
178
* @return the destination directory on success, an empty string on failure.
182
GuestCopyPasteSrc::SetupDestDir(const std::string &destDir)
186
if (!destDir.empty() && File_Exists(destDir.c_str())) {
187
mStagingDir = destDir;
188
const char *lastSep = Str_Strrchr(mStagingDir.c_str(), DIRSEPC);
189
if (lastSep && lastSep[1] != '\0') {
190
mStagingDir += DIRSEPS;
195
newDir = DnD_CreateStagingDirectory();
196
if (newDir != NULL) {
197
mStagingDir = newDir;
199
char *lastSep = Str_Strrchr(newDir, DIRSEPC);
200
if (lastSep && lastSep[1] != '\0') {
201
mStagingDir += DIRSEPS;
204
Debug("%s: destdir: %s", __FUNCTION__, mStagingDir.c_str());
206
Debug("%s: destdir not created", __FUNCTION__);