aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/barrier/DragInformation.cpp
blob: db28f3da4d9c837b99cc24eb4e4f7385fd540ea5 (plain) (blame)
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
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2013-2016 Symless 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 LICENSE 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/>.
 */

#include "barrier/DragInformation.h"
#include "base/Log.h"

#include <fstream>
#include <sstream>
#include <stdexcept>

using namespace std;

DragInformation::DragInformation() :
    m_filename(),
    m_filesize(0)
{
}

void
DragInformation::parseDragInfo(DragFileList& dragFileList, UInt32 fileNum, String data)
{
    size_t startPos = 0;
    size_t findResult1 = 0;
    size_t findResult2 = 0;
    dragFileList.clear();
    String slash("\\");
    if (data.find("/", startPos) != string::npos) {
        slash = "/";
    }
    
    UInt32 index = 0;
    while (index < fileNum) {
        findResult1 = data.find(',', startPos);
        findResult2 = data.find_last_of(slash, findResult1);

        if (findResult1 == startPos) {
            //TODO: file number does not match, something goes wrong
            break;
        }
        
        // set filename
        if (findResult1 - findResult2 > 1) {
            String filename = data.substr(findResult2 + 1,
                findResult1 - findResult2 - 1);
            DragInformation di;
            di.setFilename(filename);
            dragFileList.push_back(di);
        }
        startPos = findResult1 + 1;
        
        //set filesize
        findResult2 = data.find(',', startPos);
        if (findResult2 - findResult1 > 1) {
            String filesize = data.substr(findResult1 + 1,
                                           findResult2 - findResult1 - 1);
            size_t size = stringToNum(filesize);
            dragFileList.at(index).setFilesize(size);
        }
        startPos = findResult1 + 1;
        
        ++index;
    }

    LOG((CLOG_DEBUG "drag info received, total drag file number: %i",
        dragFileList.size()));

    for (size_t i = 0; i < dragFileList.size(); ++i) {
        LOG((CLOG_DEBUG "dragging file %i name: %s",
            i + 1,
            dragFileList.at(i).getFilename().c_str()));
    }
}

String
DragInformation::getDragFileExtension(String filename)
{
    size_t findResult = string::npos;
    findResult = filename.find_last_of(".", filename.size());
    if (findResult != string::npos) {
        return filename.substr(findResult + 1, filename.size() - findResult - 1);
    }
    else {
        return "";
    }
}

int
DragInformation::setupDragInfo(DragFileList& fileList, String& output)
{
    int size = static_cast<int>(fileList.size());
    for (int i = 0; i < size; ++i) {
        output.append(fileList.at(i).getFilename());
        output.append(",");
        String filesize = getFileSize(fileList.at(i).getFilename());
        output.append(filesize);
        output.append(",");
    }
    return size;
}

bool
DragInformation::isFileValid(String filename)
{
    bool result = false;
    std::fstream file(filename.c_str(), ios::in|ios::binary);

    if (file.is_open()) {
        result = true;
    }

    file. close();

    return result;
}

size_t
DragInformation::stringToNum(String& str)
{
    istringstream iss(str.c_str());
    size_t size;
    iss >> size;
    return size;
}

String
DragInformation::getFileSize(String& filename)
{
    std::fstream file(filename.c_str(), ios::in|ios::binary);

    if (!file.is_open()) {
      throw std::runtime_error("failed to get file size");
    }

    // 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();
}