2013-08-30 14:38:43 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2013 Bolton Software Ltd.
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package 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
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "synergy/DragInformation.h"
|
|
|
|
#include "base/Log.h"
|
2013-08-30 14:38:43 +00:00
|
|
|
|
2014-05-06 18:26:47 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2014-05-06 19:23:40 +00:00
|
|
|
#include <stdexcept>
|
2014-05-06 18:26:47 +00:00
|
|
|
|
2013-08-30 14:38:43 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2014-05-06 18:26:47 +00:00
|
|
|
CDragInformation::CDragInformation() :
|
|
|
|
m_filename(),
|
|
|
|
m_filesize(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-30 14:38:43 +00:00
|
|
|
void
|
|
|
|
CDragInformation::parseDragInfo(CDragFileList& dragFileList, UInt32 fileNum, CString data)
|
|
|
|
{
|
|
|
|
size_t startPos = 0;
|
|
|
|
size_t findResult1 = 0;
|
|
|
|
size_t findResult2 = 0;
|
|
|
|
dragFileList.clear();
|
2013-08-30 19:49:38 +00:00
|
|
|
CString slash("\\");
|
2014-02-26 15:53:28 +00:00
|
|
|
if (data.find("/", startPos) != string::npos) {
|
2013-08-30 19:49:38 +00:00
|
|
|
slash = "/";
|
|
|
|
}
|
2014-05-06 18:26:47 +00:00
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
while (index < fileNum) {
|
|
|
|
findResult1 = data.find(',', startPos);
|
2013-08-30 19:49:38 +00:00
|
|
|
findResult2 = data.find_last_of(slash, findResult1);
|
2013-08-30 14:38:43 +00:00
|
|
|
|
|
|
|
if (findResult1 == startPos) {
|
|
|
|
//TODO: file number does not match, something goes wrong
|
|
|
|
break;
|
|
|
|
}
|
2014-05-06 18:26:47 +00:00
|
|
|
|
|
|
|
// set filename
|
2013-08-30 14:38:43 +00:00
|
|
|
if (findResult1 - findResult2 > 1) {
|
2014-05-06 18:26:47 +00:00
|
|
|
CString filename = data.substr(findResult2 + 1,
|
|
|
|
findResult1 - findResult2 - 1);
|
|
|
|
CDragInformation di;
|
|
|
|
di.setFilename(filename);
|
|
|
|
dragFileList.push_back(di);
|
2013-08-30 14:38:43 +00:00
|
|
|
}
|
|
|
|
startPos = findResult1 + 1;
|
2014-05-06 18:26:47 +00:00
|
|
|
|
|
|
|
//set filesize
|
|
|
|
findResult2 = data.find(',', startPos);
|
|
|
|
if (findResult2 - findResult1 > 1) {
|
|
|
|
CString filesize = data.substr(findResult1 + 1,
|
|
|
|
findResult2 - findResult1 - 1);
|
|
|
|
size_t size = stringToNum(filesize);
|
|
|
|
dragFileList.at(index).setFilesize(size);
|
|
|
|
}
|
|
|
|
startPos = findResult1 + 1;
|
|
|
|
|
|
|
|
++index;
|
2013-08-30 14:38:43 +00:00
|
|
|
}
|
2014-04-30 18:54:32 +00:00
|
|
|
|
2014-05-06 18:26:47 +00:00
|
|
|
LOG((CLOG_DEBUG "drag info received, total drag file number: %i",
|
|
|
|
dragFileList.size()));
|
2014-04-30 18:54:32 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < dragFileList.size(); ++i) {
|
2014-05-06 18:26:47 +00:00
|
|
|
LOG((CLOG_DEBUG2 "dragging file %i name: %s",
|
|
|
|
i + 1,
|
|
|
|
dragFileList.at(i).getFilename().c_str()));
|
2014-04-30 18:54:32 +00:00
|
|
|
}
|
2013-08-30 14:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CString
|
|
|
|
CDragInformation::getDragFileExtension(CString fileName)
|
|
|
|
{
|
2014-02-26 15:53:28 +00:00
|
|
|
size_t findResult = string::npos;
|
2013-08-30 14:38:43 +00:00
|
|
|
findResult = fileName.find_last_of(".", fileName.size());
|
2014-02-26 15:53:28 +00:00
|
|
|
if (findResult != string::npos) {
|
2013-08-30 14:38:43 +00:00
|
|
|
return fileName.substr(findResult + 1, fileName.size() - findResult - 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 18:26:47 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
CDragInformation::setupDragInfo(CDragFileList& fileList, CString& output)
|
|
|
|
{
|
|
|
|
int size = fileList.size();
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
output.append(fileList.at(i).getFilename());
|
|
|
|
output.append(",");
|
|
|
|
CString filesize = getFileSize(fileList.at(i).getFilename());
|
|
|
|
output.append(filesize);
|
|
|
|
output.append(",");
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
CDragInformation::stringToNum(CString& str)
|
|
|
|
{
|
|
|
|
istringstream iss(str.c_str());
|
|
|
|
size_t size;
|
|
|
|
iss >> size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
CString
|
|
|
|
CDragInformation::getFileSize(CString& filename)
|
|
|
|
{
|
2014-05-06 19:23:40 +00:00
|
|
|
std::fstream file(filename.c_str(), ios::in|ios::binary);
|
2014-05-06 18:26:47 +00:00
|
|
|
|
|
|
|
if (!file.is_open()) {
|
2014-05-06 19:23:40 +00:00
|
|
|
throw std::runtime_error("failed to get file size");
|
2014-05-06 18:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check file size
|
|
|
|
file.seekg (0, std::ios::end);
|
|
|
|
size_t size = (size_t)file.tellg();
|
|
|
|
|
|
|
|
stringstream ss;
|
|
|
|
ss << size;
|
|
|
|
|
|
|
|
file. close();
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|