1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
|
/*********************************************************
* Copyright (C) 2007 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation version 2.1 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*********************************************************/
/*
* dndFileList.cc
*
* Handles filelists for CPClipboard.
* Relative paths are read/write to allow placing data on the clipboard,
* but full paths are write only. Full path parsing depends on g->h/h->g
* as well as dnd/fcp versions. Given that the host ui never needs to
* parse it, full paths are only stored in binary format for consumption
* by the vmx.
*
* Local relative paths are expected to be encoded in Normalized UTF8 in
* local format.
*
* XXX This file is almost identical to bora/apps/lib/cui/dndFileList.cc
* but right now we can not find a way to share the file.
*/
#include "dndFileList.hh"
extern "C" {
#include "dndClipboard.h"
#include "vm_assert.h"
#include "file.h"
#include "cpNameUtil.h"
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList --
*
* Constructor.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
DnDFileList::DnDFileList()
{
mFileSize = 0;
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::SetFileSize --
*
* Sets the expected size of the files.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
void
DnDFileList::SetFileSize(uint64 fsize) // IN: The size of the files.
{
mFileSize = fsize;
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::GetFileSize --
*
* Gets the size of the files if known.
*
* Results:
* 0 if the size of the files is not known, otherwise the expected size of
* the files.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
uint64
DnDFileList::GetFileSize() const
{
return mFileSize;
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::AddFile --
*
* Add the full path and the relative path to the file list. Both strings
* should be normalized UTF8.
* Will not add file if the file list was created from a clipboard buffer.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
void
DnDFileList::AddFile(const std::string fullPath, // IN: filename
const std::string relPath) // IN: filename
{
ASSERT(mFullPathsBinary.empty());
if (!mFullPathsBinary.empty()) {
return;
}
mRelPaths.push_back(relPath);
mFullPaths.push_back(fullPath);
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::AddFileUri --
*
* Add the full uri UTF8 path the file list.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
void
DnDFileList::AddFileUri(const std::string uriPath) // IN
{
mUriPaths.push_back(uriPath);
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::AddFiles --
*
* Add the full path list and the relative path list. Both lists should be
* normalized UTF8.
*
* Will not add lists if the file list was created from a clipboard buffer.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
void
DnDFileList::AddFiles(const std::vector<std::string> fullPathList, // IN: filenames
const std::vector<std::string> relPathList) // IN: filenames
{
ASSERT(mFullPathsBinary.empty());
if (!mFullPathsBinary.empty()) {
return;
}
mRelPaths = relPathList;
mFullPaths = fullPathList;
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::SetRelPathsStr --
*
* Set the relative paths based on the serialized input string. Paths are
* in normalized utf-8 with local path separators. Exposed for dnd/cp
* version 2.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
void
DnDFileList::SetRelPathsStr(const std::string inpath) // IN:
{
std::string::size_type mPos = 0;
std::string curFile;
std::string path;
const char* cpath;
if (inpath.empty()) {
return;
}
if (inpath[inpath.size()-1] != '\0') {
path = inpath + '\0';
} else {
path = inpath;
}
cpath = path.c_str();
mRelPaths.clear();
curFile = cpath;
mPos = path.find('\0', mPos);
while (mPos != std::string::npos) {
mPos++;
mRelPaths.push_back(curFile);
curFile = cpath + mPos;
mPos = path.find('\0', mPos);
}
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::GetRelPaths --
*
* Gets a vector containing the reletive paths of the files.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
std::vector<std::string>
DnDFileList::GetRelPaths() const
{
return mRelPaths;
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::GetFullPathsStr --
*
* Gets the serialized version of the full paths. If the file list was
* created from a CPClipboard, use the serialized input instead.
*
* Local paths are serialized inot NUL separated pathes.
* CPName paths are converted from local to CPName and serialized into
* uint32,cpname pairs.
*
* Results:
* An std::string containing the serialized full paths. Empty string on
* error.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
std::string
DnDFileList::GetFullPathsStr(bool local) // IN: Use local format
const
{
std::string stringList;
std::vector<std::string>::const_iterator i;
if (mFullPathsBinary.empty() && !mFullPaths.empty()) {
for (i = mFullPaths.begin(); i != mFullPaths.end(); ++i) {
if (local) {
stringList.append(i->c_str());
stringList.push_back('\0');
} else {
char outPath[FILE_MAXPATH + 100];
int32 outPathLen;
outPathLen = CPNameUtil_ConvertToRoot(i->c_str(),
sizeof outPath,
outPath);
if (outPathLen < 0) {
continue;
}
stringList.append(reinterpret_cast<char *>(&outPathLen),
sizeof outPathLen);
stringList.append(outPath, outPathLen);
}
}
return stringList;
} else if (!mFullPathsBinary.empty() && mFullPaths.empty()) {
return mFullPathsBinary;
} else {
return "";
}
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::GetRelPathsStr --
*
* Gets the serialized version of the relative paths. Primarily used for
* dnd/cp v2.
*
* Results:
* An std::string contatin the serialized full paths.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
std::string
DnDFileList::GetRelPathsStr()
const
{
std::string stringList;
std::vector<std::string>::const_iterator i;
for (i = mRelPaths.begin(); i != mRelPaths.end(); ++i) {
stringList.append(i->c_str());
stringList.push_back('\0');
}
return stringList;
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::GetUriPathsStr --
*
* Gets the serialized version of the uri paths.
*
* Results:
* An std::string contatin the serialized uri paths.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
std::string
DnDFileList::GetUriPathsStr(void)
const
{
std::string stringList;
std::vector<std::string>::const_iterator i;
for (i = mUriPaths.begin(); i != mUriPaths.end(); i++) {
stringList.append(i->c_str());
stringList.push_back('\0');
}
return stringList;
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::FromCPClipboard --
*
* Loads the filelist from a buffer, typically from CPClipboard.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
bool
DnDFileList::FromCPClipboard(const void *buf, // IN: Source buffer
size_t len) // IN: Buffer length
{
const CPFileList *flist;
std::string relPaths;
ASSERT(buf);
ASSERT(len);
if (!buf || !len) {
return false;
}
flist = reinterpret_cast<const CPFileList *>(buf);
relPaths.assign(
reinterpret_cast<const char *>(flist->filelists), flist->relPathsLen);
mRelPaths.clear();
mFullPaths.clear();
mFileSize = flist->fileSize;
SetRelPathsStr(relPaths);
mFullPathsBinary.assign(
reinterpret_cast<const char *>(flist->filelists + flist->relPathsLen),
flist->fulPathsLen);
return true;
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::ToCPClipboard --
*
* Serializes contents for CPClipboard in eithr CPFormat or local format.
*
* Results:
* false on error, true on success.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
bool
DnDFileList::ToCPClipboard(DynBuf *out, // OUT: Initialized output buffer
bool local) // IN: Use local format
const
{
std::string strListRel;
std::string strListFul;
CPFileList header;
strListRel = GetRelPathsStr();
strListFul = GetFullPathsStr(local);
if (!out) {
return false;
}
/* Check if the size is too big. */
if (strListRel.size() > MAX_UINT32 ||
strListFul.size() > MAX_UINT32) {
return false;
}
header.fileSize = mFileSize;
header.relPathsLen = strListRel.size();
header.fulPathsLen = strListFul.size();
DynBuf_Append(out, &header, CPFILELIST_HEADER_SIZE);
DynBuf_Append(out, strListRel.c_str(), header.relPathsLen);
DynBuf_Append(out, strListFul.c_str(), header.fulPathsLen);
return true;
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::ToCPClipboard --
*
* Serializes uri paths for CPClipboard in URI Format.
*
* Results:
* false on error, true on success.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
bool
DnDFileList::ToUriClipboard(DynBuf *out) // OUT: Initialized output buffer
const
{
std::string strListUri;
UriFileList header;
if (!out) {
return false;
}
strListUri = GetUriPathsStr();
/* Check if the size is too big. */
if (strListUri.size() > MAX_UINT32) {
return false;
}
header.fileSize = mFileSize;
header.uriPathsLen = strListUri.size();
DynBuf_Append(out, &header, URI_FILELIST_HEADER_SIZE);
DynBuf_Append(out, strListUri.c_str(), header.uriPathsLen);
return true;
}
/*
*----------------------------------------------------------------------------
*
* DnDFileList::Clear --
*
* Clears the contents of the filelist.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
void
DnDFileList::Clear()
{
mRelPaths.clear();
mFullPaths.clear();
mUriPaths.clear();
mFullPathsBinary.clear();
mFileSize = 0;
}
|