Debian Codegraph @

src/RsyncDirModel.cpp

raw symbols SCIP
1 /****************************************************************************
2 Copyright (C) VCreate Logic Private Limited, Bangalore
3 Contact: [email protected]
4
5 This file is developed by VCreate Logic Private Limited and used as part of the "luckyBackup" project
6 It is distributed under the terms of the GNU General Public License
7 It is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 It is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with the luckyBackup project. If not, see <http://www.gnu.org/licenses/>.
19
20 ****************************************************************************/
21 // modified on 22 May 2016
22 // by luckyb after Lothar's fixes
23
24 #include "RsyncDirModel.h"
25 #include "RsyncDirModel_p.h"
26
27 #include <QIcon>
28 #include <QStyle>
29 #include <QApplication>
30 #include <QProcess>
31 #include <QTextStream>
32 #include <QString>
33 #include <QDebug>
34
35 struct RsyncDirModelData
36 {
37 // QUrl url;
38 // RsyncDirLister* lister;
39 QList<RsyncEntry*> topLevelEntries;
40 bool topLevelEntriesFetched;
41 // QString command; //command to be executed. Normally this is "rsync"
42 // QStringList rsyncArguments; // This stringList holds all arguments for the rsync command
43 // QProcess *syncProcess;
44 // bool ProcReportedError;
45 RsyncDirLister* lister;
46 };
47
48 RsyncDirModel::RsyncDirModel(QObject* parent): QAbstractItemModel(parent)
49 {
50 d = new RsyncDirModelData;
51 // d->syncProcess = new QProcess(this);
52 // d->rsyncArguments.clear();
53 // d->rsyncArguments << "--list-only";
54 // d->command = "rsync";
55 d->lister = new RsyncDirLister;
56
57
58 // connect(d->lister, SIGNAL(loginParamsRequest(QAuthenticator*)),
59 // this, SIGNAL(loginParamsRequest(QAuthenticator*)));
60 connect(d->lister, SIGNAL(listingDone(RsyncEntry*)),
61 this, SLOT(slotListingDone(RsyncEntry*)));
62 // connect(syncProcess, SIGNAL (finished(int, QProcess::ExitStatus)), this, SLOT(procFinished()));
63 // connect(syncProcess, SIGNAL (error(QProcess::ProcessError)), this, SLOT(procError()));
64
65 // d->ProcReportedError = false; // This might change as soon as syncprocess will start ()
66 }
67
68 RsyncDirModel::~RsyncDirModel()
69 {
70 delete d;
71 }
72
73 void RsyncDirModel::setUrl(const QUrl& url,const QString& cpath)
74 {
75 d->lister->setUrl(url,cpath);
76 d->lister->list();
77 }
78
79 void RsyncDirModel::setUrl(const QUrl& url,const QString& cpath, const QStringList& cargs)
80 {
81 d->lister->setUrl(url,cpath,cargs);
82
83 d->lister->list();
84 }
85
86 void RsyncDirModel::setUrl(const QUrl& url)
87 {
88 d->lister->setUrl(url);
89 d->lister->list();
90 }
91
92 QUrl RsyncDirModel::url() const
93 {
94 return d->lister->url();
95 }
96
97 QUrl RsyncDirModel::url(const QModelIndex& index) const
98 {
99 QString path = this->path(index);
100
101 QUrl url = d->lister->url();
102 url.setPath(path);
103 return url;
104 }
105
106 QString RsyncDirModel::path(const QModelIndex& index) const
107 {
108 RsyncEntry* entry = (RsyncEntry*)index.internalPointer();
109 if(!entry)
110 return QString();
111
112 QString rootPath = d->lister->url().path();
113 QString pathStr = rootPath + entry->path();
114 return pathStr;
115 }
116
117 QModelIndex RsyncDirModel::index(const QString& path) const
118 {
119 if(!d->topLevelEntries.count())
120 return QModelIndex();
121
122 // Remove the root path name from the input
123 // if given
124 QString path2 = path;
125 QUrl url = d->lister->url();
126 QString rootPath = url.path();
127 if(path2.startsWith(rootPath))
128 path2.remove(0, rootPath.length());
129
130 // Break the path name into directory/file names.
131 QStringList comps = path2.split("/", QString::SkipEmptyParts);
132
133 // Scan entries and look for the path
134 QList<RsyncEntry*> entryList = d->topLevelEntries;
135 RsyncEntry* entry = 0;
136 for(int i=0; i<comps.count(); i++)
137 {
138 for(int i=0; i<entryList.count(); i++)
139 {
140 entry = 0;
141
142 RsyncEntry* e = entryList.at(i);
143 if(e->urlInfo().name() == comps.at(i))
144 {
145 entry = e;
146 break;
147 }
148 }
149
150 if(!entry)
151 return QModelIndex();
152
153 entryList = entry->childEntries();
154 }
155
156 if(entry)
157 return index(entry);
158
159 return QModelIndex();
160 }
161
162 void RsyncDirModel::refresh(const QModelIndex& index)
163 {
164 if(index.isValid())
165 {
166 RsyncEntry* entry = (RsyncEntry*)index.internalPointer();
167 entry->setChildrenFetched(false);
168 d->lister->list(entry);
169 }
170 else
171 {
172 beginResetModel();
173 d->topLevelEntries.clear();
174 d->lister->list();
175 endResetModel();
176 }
177 }
178
179 int RsyncDirModel::rowCount(const QModelIndex& parent) const
180 {
181 if(!parent.isValid())
182 {
183 if(d->topLevelEntries.count())
184 return d->topLevelEntries.count();
185
186 // Return one row to show that entries are being fetched.
187 // Or that entries cant be fetched.
188 return 1;
189 }
190
191 if(!d->topLevelEntries.count())
192 return 0;
193
194 RsyncEntry* entry = (RsyncEntry*)parent.internalPointer();
195 if(entry)
196 {
197 if(entry->isChildrenFetched())
198 return entry->childEntries().count();
199
200 return 1;
201 }
202
203 return 0;
204 }
205
206 int RsyncDirModel::columnCount(const QModelIndex& parent) const
207 {
208 Q_UNUSED(parent);
209
210 if(d->topLevelEntries.count())
211 return 3;
212
213 // Return one row to show that entries are being fetched.
214 // Or that entries cant be fetched.
215 return 1;
216 }
217
218 QVariant RsyncDirModel::data(const QModelIndex& index, int role) const
219 {
220 if(!index.isValid())
221 return QVariant();
222
223 if(!d->topLevelEntries.count())
224 {
225 if(!index.row() && !index.column() && role == Qt::DisplayRole)
226 {
227 if(d->topLevelEntriesFetched)
228 return QString("Could not fetch %1").arg(d->lister->url().toString());
229 else
230 return QString("Fetching %1...").arg(d->lister->url().toString());
231 }
232 return QVariant();
233 }
234
235 RsyncEntry* entry = (RsyncEntry*)index.internalPointer();
236 if(!entry->urlInfo().isValid())
237 {
238 if(!index.column() && role == Qt::DisplayRole)
239 return "Fetching...";
240
241 return QVariant();
242 }
243
244 switch(index.column())
245 {
246 case 0: // Name
247 if(role == Qt::DisplayRole)
248 {
249 return entry->urlInfo().name();
250 }
251
252 if(role == Qt::DecorationRole)
253 return entry->icon();
254
255 if(role == Qt::ToolTipRole)
256 return entry->path();
257
258 break;
259
260 case 1: // Last Modified
261 if(role == Qt::DisplayRole)
262 return entry->urlInfo().lastModified().toString();
263
264 break;
265
266 case 2: // Size
267 if(role == Qt::DisplayRole)
268 {
269 if(!entry->urlInfo().isDir())
270 return entry->urlInfo().size();
271 }
272
273 break;
274 }
275
276 return QVariant();
277 }
278
279 QModelIndex RsyncDirModel::index(int row, int col, const QModelIndex& parent) const
280 {
281 if(!parent.isValid())
282 {
283 if(!d->topLevelEntries.count())
284 return createIndex(row, col);
285
286 if(row < 0 || row >= d->topLevelEntries.count())
287 return QModelIndex();
288
289 RsyncEntry* entry = d->topLevelEntries.at(row);
290 return createIndex(row, col, entry);
291 }
292
293 RsyncEntry* entry = (RsyncEntry*)parent.internalPointer();
294 if(!entry->isChildrenFetched())
295 d->lister->list(entry);
296
297 if(row < 0 || row >= entry->childEntries().count())
298 return QModelIndex();
299
300 RsyncEntry* childEntry = entry->childEntries().at(row);
301 return createIndex(row, col, childEntry);
302 }
303
304 QModelIndex RsyncDirModel::parent(const QModelIndex& index) const
305 {
306 if(!index.isValid() || !d->topLevelEntries.count())
307 return QModelIndex();
308
309 RsyncEntry* entry = (RsyncEntry*)index.internalPointer();
310 RsyncEntry* parentRowEntry = entry->parentEntry();
311
312 if(!parentRowEntry)
313 return QModelIndex();
314
315 int idx = d->topLevelEntries.indexOf(parentRowEntry);
316 if(idx >= 0)
317 return createIndex(idx, 0, parentRowEntry);
318
319 RsyncEntry* grantParent = parentRowEntry->parentEntry();
320 idx = grantParent->childEntries().indexOf(parentRowEntry);
321 return createIndex(idx, 0, parentRowEntry);
322 }
323
324 QVariant RsyncDirModel::headerData(int section, Qt::Orientation orientation, int role) const
325 {
326 if( role != Qt::DisplayRole || orientation != Qt::Horizontal )
327 return QVariant();
328
329 if(!d->topLevelEntries.count())
330 return QString();
331
332 switch(section)
333 {
334 case 0:
335 return "Name";
336 case 1:
337 return "Last Modified";
338 case 2:
339 return "Size";
340 default:
341 break;
342 }
343
344 return QVariant();
345 }
346
347 Qt::ItemFlags RsyncDirModel::flags(const QModelIndex& index) const
348 {
349 RsyncEntry* entry = (RsyncEntry*)index.internalPointer();
350 if(entry && entry->urlInfo().isValid())
351 return Qt::ItemIsSelectable|Qt::ItemIsEnabled;
352
353 return Qt::ItemIsEnabled;
354 }
355
356 QModelIndex RsyncDirModel::index(RsyncEntry* entry) const
357 {
358 // Confirm that this entry belongs to us
359 if(!d->topLevelEntries.count())
360 return QModelIndex();
361
362 RsyncEntry* e2 = entry;
363 while(e2->parentEntry())
364 e2 = e2->parentEntry();
365
366 if(!d->topLevelEntries.contains(e2))
367 return QModelIndex();
368
369 RsyncEntry* parent = entry->parentEntry();
370 if(parent)
371 return createIndex(parent->childEntries().indexOf(entry), 0, entry);
372
373 return createIndex(d->topLevelEntries.indexOf(entry), 0, entry);
374 }
375
376 void RsyncDirModel::slotListingDone(RsyncEntry* entry)
377 {
378 if(entry)
379 {
380 QModelIndex index = this->index(entry);
381 if(!index.isValid())
382 return;
383
384 beginRemoveRows(index, 0, 1);
385 // Do nothing
386 endRemoveRows();
387
388 beginInsertRows(index, 0, entry->childEntries().count());
389 // Do nothing
390 endInsertRows();
391 }
392 else
393 {
394 beginResetModel();
395 d->topLevelEntries = d->lister->topLevelEntries();
396 d->topLevelEntriesFetched = true;
397 endResetModel();
398 }
399 }
400
401
402