-
Notifications
You must be signed in to change notification settings - Fork 2
/
drfileitem.cpp
201 lines (144 loc) · 4.4 KB
/
drfileitem.cpp
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
/*
Deriv is a cross-platform client for th Wired 2.0 protocol
Copyright (C) 2014 Rafael Warnault, rw@read-write.fr
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any 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
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 <QDebug>
#include <QFileInfo>
#include "drfileitem.h"
#include "drfilestreemodel.h"
#include "dr.h"
#include "main.h"
#include <QStringList>
DRFileItem::DRFileItem(wi_p7_message_t *message, DRServerConnection *connection, DRFileItem *parent, QModelIndex index)
{
this->connection = connection;
this->parentItem = parent;
this->index = index;
this->type = DRFileDirectory;
if(message != NULL) {
wi_string_t *string = NULL;
wi_date_t *date = NULL;
wi_p7_enum_t wenum;
string = wi_p7_message_string_for_name(message, WI_STR("wired.file.path"));
if(string != NULL) {
this->path = WSTOQS(string);
this->name = QFileInfo(this->path).fileName();
}
date = wi_p7_message_date_for_name(message, WI_STR("wired.file.creation_time"));
if(date != NULL)
this->creationTime = DR::wiredDateToQDateTime(date);
wi_p7_message_get_enum_for_name(message, &wenum, WI_STR("wired.file.type"));
this->type = (DRFileType)wenum;
}
}
DRFileItem::~DRFileItem()
{
qDeleteAll(childItems);
}
DRFileItem *DRFileItem::child(int number)
{
return childItems.value(number);
}
int DRFileItem::childCount() const
{
return childItems.count();
}
int DRFileItem::childNumber() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<DRFileItem*>(this));
return 0;
}
int DRFileItem::columnCount() const
{
return 3;
}
QVariant DRFileItem::data(int column) const
{
if(column == 0)
return this->name;
else if (column == 1)
return this->creationTime.toString();
else if (column == 2)
return this->dataSize;
return this->name;
}
QString DRFileItem::getName() {
return this->name;
}
QString DRFileItem::getPath() {
return this->path;
}
QModelIndex DRFileItem::getIndex() {
return this->index;
}
DRFileType DRFileItem::getType() {
return this->type;
}
bool DRFileItem::appendChild(DRFileItem *item) {
this->childItems.append(item);
return true;
}
bool DRFileItem::cleanChildren() {
this->childItems.clear();
}
bool DRFileItem::insertChildren(int position, int count, int columns)
{
if (position < 0 || position > childItems.size())
return false;
for (int row = 0; row < count; ++row) {
// QVector<QVariant> data(columns);
// DRFileItem *item = new DRFileItem(data, this);
// childItems.insert(position, item);
}
return true;
}
bool DRFileItem::insertColumns(int position, int columns)
{
if (position < 0 || position > itemData.size())
return false;
for (int column = 0; column < columns; ++column)
itemData.insert(position, QVariant());
foreach (DRFileItem *child, childItems)
child->insertColumns(position, columns);
return true;
}
DRFileItem *DRFileItem::parent()
{
return parentItem;
}
bool DRFileItem::removeChildren(int position, int count)
{
if (position < 0 || position + count > childItems.size())
return false;
for (int row = 0; row < count; ++row)
delete childItems.takeAt(position);
return true;
}
bool DRFileItem::removeColumns(int position, int columns)
{
if (position < 0 || position + columns > itemData.size())
return false;
for (int column = 0; column < columns; ++column)
itemData.remove(position);
foreach (DRFileItem *child, childItems)
child->removeColumns(position, columns);
return true;
}
bool DRFileItem::setData(int column, const QVariant &value)
{
if (column < 0 || column >= itemData.size())
return false;
itemData[column] = value;
return true;
}