-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsuggestnsbasewin.cpp
More file actions
445 lines (403 loc) · 16 KB
/
suggestnsbasewin.cpp
File metadata and controls
445 lines (403 loc) · 16 KB
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
#include "suggestnsbasewin.h"
#include "ui_suggestnsbasewin.h"
#include "addsyncdialog.h"
#include "modifysyncdialog.h"
#include "ui_modifysyncdialog.h"
#include "psynclib.h"
#include "common.h"
#include "unistd.h"
#include <QDebug>
#include <QComboBox>
#include <QStandardItemModel>
//this class is used from windows context menu for adding many folders at the same time for sync
const char* typeStr[3]={"Download only", "Upload only", "Download and Upload"};
SuggestnsBaseWin::SuggestnsBaseWin(PCloudApp *a, bool addlocal, QStringList *fldrs, QWidget *parent):
QMainWindow(parent),
ui(new Ui::SuggestnsBaseWin)
{
app = a;
ui->setupUi(this);
isChangingItem = false;
addFldrsLst = fldrs;
addLocalFldrsFlag = addlocal;
ui->label_syncinfo->setText("With Sync you can synchronize\n"
"the content in multiple folders from your\n"
"computer in real time. By synchronizing\n"
"folders in pCloud Drive you make them\navailable even in offline mode.");
#ifdef Q_OS_WIN
dfltDir = new QDir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
#else
dfltDir = new QDir(QDir::homePath());
#endif
connect(ui->btnAdd, SIGNAL(clicked()),this, SLOT(addSync()));
connect(ui->btnFinish, SIGNAL(clicked()), this, SLOT(finish()));
connect(ui->btnCancel, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->treeWidget, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(changeCurrItem(QModelIndex)));
ui->statusbar->setVisible(false);
this->setWindowTitle(trUtf8("pCloud Drive"));
this->setWindowIcon(QIcon(WINDOW_ICON));
//default synced fldrs
QString root = "/";
pfolder_list_t *remoteRootChildFldrs = psync_list_remote_folder_by_path(root.toUtf8(),PLIST_FOLDERS);
//build list with remote root child names in order to check: when we add new sync delay if the folderName exists.
for (uint i = 0; i < remoteRootChildFldrs->entrycnt; i++)
remoteFldrsNamesLst.append(remoteRootChildFldrs->entries[i].name);
qDebug()<<"Remote init folders list form lib"<<remoteFldrsNamesLst;
/*
QString defaultRemoteFldr = "/pCloud Sync";
QTreeWidgetItem *defaultItem = new QTreeWidgetItem(ui->treeWidget); // the default sync; the first item in the view; uneditable
defaultItem->setCheckState(0,Qt::Checked);
defaultItem->setFlags(Qt::NoItemFlags);
QString path = QDir::home().path().append("/pCloud Sync");
QDir pcloudDir(path);
QList<QStringList> itemsLst;
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
QDir docs (QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation));
QDir music(QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
QDir photos(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation));
QDir movies(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
#else
QDir docs(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
QDir music(QStandardPaths::writableLocation(QStandardPaths::MusicLocation));
QDir photos(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
QDir movies(QStandardPaths::writableLocation(QStandardPaths::MoviesLocation));
#endif
QString nativepath;
#ifdef Q_OS_WIN
QString docspath;
nativepath = pcloudDir.toNativeSeparators(path);
docspath = docs.toNativeSeparators(docs.absolutePath());
itemsLst << (QStringList() << docspath << "/My Documents");
// in win xp My music, My pictures and My videos are located in My documents by default
if (QSysInfo::windowsVersion() != QSysInfo::WV_XP)
{
QString musicpath,moviespath, photospath;
musicpath = music.toNativeSeparators(music.absolutePath());
moviespath = movies.toNativeSeparators(music.absolutePath());
photospath = photos.toNativeSeparators(photos.absolutePath());
itemsLst << (QStringList() << musicpath << "/My Music")
<< (QStringList() << photospath << "/My Pictures")
<< (QStringList() << moviespath << "/My Videos");
}
#else
nativepath = pcloudDir.path();
itemsLst << (QStringList() << docs.absolutePath() << "/My Documents")
<< (QStringList() << music.absolutePath() << "/My Music")
<< (QStringList() << photos.absolutePath() << "/My Pictures")
<< (QStringList() << movies.absolutePath() << "/My Videos");
#endif
if(!pcloudDir.exists())
{
QDir::home().mkdir("pCloud Sync");
remoteFldrsNamesLst.append("pCloud Sync");
newRemoteFldrsLst.append("pCloud Sync");
}
defaultItem->setText(1,nativepath);
defaultItem->setData(1,Qt::UserRole, nativepath);
defaultItem->setText(2,trUtf8("Download and Upload"));
defaultItem->setData(2,Qt::UserRole, PSYNC_FULL -1); //for combos and typestr[] indexes
defaultItem->setText(3,defaultRemoteFldr);
defaultItem->setData(3,Qt::UserRole,defaultRemoteFldr);
ui->treeWidget->insertTopLevelItem(0,defaultItem);
//remoteFldrsNamesLst.append("My Documents");
//newRemoteFldrsLst.append("My Documents");
for(int i = 0; i < itemsLst.length(); i++)
{
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
item->setCheckState(0,Qt::Unchecked);
item->setData(0,Qt::UserRole,false);
item->setText(1,itemsLst.at(i).at(0));
item->setData(1, Qt::UserRole,itemsLst.at(i).at(0));
item->setText(3, itemsLst.at(i).at(1));
item->setData(3, Qt::UserRole, itemsLst.at(i).at(1));
item->setText(2, typeStr[2]);
item->setData(2, Qt::UserRole, PSYNC_FULL - 1);
}
*/
//ui->treeWidget->resizeColumnToContents(0);
ui->treeWidget->setColumnWidth(0,40);
ui->treeWidget->setColumnWidth(1,200);
ui->treeWidget->setColumnWidth(2,30);
ui->treeWidget->resizeColumnToContents(3);
ui->treeWidget->setMinimumWidth(460);
//ui->tableWidget->setItemDelegate(new SyncItemsDelegate());
this->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
this->layout()->setSizeConstraint(QLayout::SetFixedSize); //not resized
if(addFldrsLst != NULL) //if not creating a default suggestion sync
{
if(addlocal)
addLocalFldrs(addFldrsLst);
else
addRemoteFldrs(addFldrsLst);
}
}
void SuggestnsBaseWin::addLocalFldrs(QStringList *itemsLst) //received from context menu - local FS, not drive
{
addLocalFldrsFlag = true;
qDebug()<<"SuggestnsBaseWin::addLocalFldrs";
if(ui->treeWidget->topLevelItemCount())
ui->treeWidget->clear();
if(!newRemoteFldrsLst.empty())
newRemoteFldrsLst.clear();
for(int i = 0; i < itemsLst->size(); i++)
{
qDebug()<<"Qt: fill suggestions tree local folders"<<i<<itemsLst->at(i);
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
item->setCheckState(0,Qt::Checked);
item->setData(0,Qt::UserRole,true);
item->setText(1,itemsLst->at(i)); //localpath
item->setData(1, Qt::UserRole,itemsLst->at(i));
item->setIcon(2,QIcon(":/32x32/images/32x32/sync.png"));
#ifdef Q_OS_WIN
QString name = itemsLst->at(i).section("\\",-1);
#else
QString name = itemsLst->at(i).section("/",-1);
#endif
QString remoteFldrName = checkRemoteName(name); // rename if remote folder with the same name exists
newRemoteFldrsLst.append(remoteFldrName);
// remoteFldrsNamesLst.append(remoteFldrName);
remoteFldrName.insert(0,"/");
item->setText(3,remoteFldrName); //remotepath
item->setData(3,Qt::UserRole,remoteFldrName);
}
}
void SuggestnsBaseWin::addRemoteFldrs(QStringList *itemsLst)
{
addLocalFldrsFlag = false;
qDebug()<<"SuggestnsBaseWin::addRemoteFldrs";
if(ui->treeWidget->topLevelItemCount())
ui->treeWidget->clear();
bool hasCryptoSubscr = !app->getIsCryptoExpired();
for(int i = 0; i < itemsLst->size(); i++)
{
QString remotepath = itemsLst->at(i);
if(hasCryptoSubscr && isCrypto(remotepath.toUtf8())) // don't suggest crypto fldr
continue;
qDebug()<<"fill suggestions remote folders" << remotepath;
QTreeWidgetItem* item = new QTreeWidgetItem(ui->treeWidget);
item->setCheckState(0,Qt::Checked);
item->setData(0, Qt::UserRole, true);
QString localpath, remoteFldrName = remotepath.section("/",1);
if(!dfltDir->exists(remoteFldrName))
localpath = dfltDir->path().append("/" + remoteFldrName);
else
localpath = getLocalName(remoteFldrName);
#ifdef Q_OS_WIN
localpath.replace("/","\\");
#endif
item->setText(1, localpath);
item->setData(1, Qt::UserRole, localpath);
item->setIcon(2,QIcon(":/32x32/images/32x32/sync.png"));
item->setText(3, remotepath);
item->setData(3, Qt::UserRole, remotepath);
}
}
int SuggestnsBaseWin::getItemsNum()
{
return ui->treeWidget->topLevelItemCount();
}
bool SuggestnsBaseWin::isCrypto(const char *path)
{
int res = false;
pentry_t* pfldr = psync_stat_path(path);
if (pfldr != NULL)
{
res = pfldr->folder.isencrypted;
free(pfldr);
}
return res;
}
SuggestnsBaseWin::~SuggestnsBaseWin()
{
delete ui;
}
void SuggestnsBaseWin::closeEvent(QCloseEvent *event)
{
this->hide();
event->ignore();
}
void SuggestnsBaseWin::addSync()
{
if (this->isChangingItem)
{
currentLocal = ui->treeWidget->currentItem()->data(1,Qt::UserRole).toString();
currentType = ui->treeWidget->currentItem()->data(2,Qt::UserRole).toInt();
currentRemote = ui->treeWidget->currentItem()->data(3,Qt::UserRole).toString();
}
QApplication::setOverrideCursor(Qt::WaitCursor);
addSyncDialog *addDialog = new addSyncDialog(app,app->pCloudWin, this);
QApplication::restoreOverrideCursor();
addDialog->exec();
delete addDialog;
}
//when user double-clicks on an item
void SuggestnsBaseWin::changeCurrItem(QModelIndex index)
{
//if(index.row()) // the first item in the treeview is default and shoudn't be modified
this->isChangingItem = true;
emit this->addSync();
}
bool SuggestnsBaseWin::getChangeItem()
{
return this->isChangingItem;
}
void SuggestnsBaseWin::addNewItem(QString &localpath, QString &remotepath, int type) //type is the current combo index
{
QTreeWidgetItem *item;
if(isChangingItem)
{
isChangingItem = false;
item = ui->treeWidget->currentItem();
}
else
item = new QTreeWidgetItem(ui->treeWidget);
item->setCheckState(0,Qt::Checked);
item->setData(0,Qt::UserRole,true);
item->setText(1,localpath);
item->setData(1,Qt::UserRole,localpath);
item->setText(3,remotepath);
item->setData(3,Qt::UserRole,remotepath); //da e remote path
item->setText(2,typeStr[type]);
item->setData(2,Qt::UserRole,type + 1);
}
/*
void SuggestnsBaseWin::modifyType() // obsolete behavior - sync now is only two-way
{
QTreeWidgetItem *current = ui->treeWidget->currentItem();
if(!current)
{
QMessageBox::information(this,"",trUtf8("Please select an item to modify"));
return;
}
else
{
ModifySyncDialog dialog(current->data(1,Qt::UserRole).toString(),current->data(3,Qt::UserRole).toString(), current->data(2,Qt::UserRole).toInt());
if (dialog.exec() == QDialog::Accepted)
{
int newType = dialog.returnNewType();
current->setData(2,Qt::UserRole, newType + 1);
dialog.hide();
current->setText(2,typeStr[newType]);
}
}
}
*/
void SuggestnsBaseWin::finish()
{
QApplication::setOverrideCursor(Qt::WaitCursor);
QTreeWidgetItemIterator it(ui->treeWidget);
while (*it)
{
qDebug()<<"SuggestnsBaseWin::finish";
if ((*it)->checkState(0) == Qt::Checked)
{
QString localpath = (*it)->data(1,Qt::UserRole).toString(), remotepath = (*it)->data(3,Qt::UserRole).toString();
qDebug()<<"ADDING item (from suggestionswin) " << (*it)->data(1,Qt::UserRole).toString().toUtf8() << (*it)->data(3,Qt::UserRole).toString().toUtf8() <<((*it)->data(2,Qt::UserRole).toInt())
<<addLocalFldrsFlag<< psync_fs_isstarted() ;
if(!addLocalFldrsFlag && !QDir(localpath).exists() // when add remote folders from drive to sync => create local if don't exist
&& QDir().mkdir(localpath));
//if add local fs folders from context menu and the suggested remove doesn't exist
//else if(addLocalFldrsFlag && !remoteFldrsNamesLst.contains(remotepath.right(remotepath.size() - 1)))
else if(addLocalFldrsFlag && newRemoteFldrsLst.contains(remotepath.right(remotepath.size() - 1)))
//&& psync_fs_isstarted() &&
// !QDir(QString(psync_fs_getmountpoint() + remotepath)).exists())
{
qDebug()<<"create remote folder";
char* err = NULL;
psync_create_remote_folder_by_path(remotepath.toUtf8(),&err);
if(err)
qDebug()<<"SuggestnsBaseWin::finish() Create new remote fldr ERR"<<err;
sleep(2);
}
/*
//welcome win case and contex menu case, because it's possible remote folder not to exists
psync_add_sync_by_path_delayed(localpath.toUtf8(), // works only for Status CONNECTING
//(*it)->data(1,Qt::UserRole).toString().toUtf8(),
(*it)->data(3,Qt::UserRole).toString().toUtf8(),
(*it)->data(2,Qt::UserRole).toInt());
++it;
*/
psync_syncid_t id = psync_add_sync_by_path(localpath.toUtf8(),
(*it)->data(3,Qt::UserRole).toString().toUtf8(),
PSYNC_FULL);
if (id == -1)
{
app->check_error();
++it;
continue;
}
#ifdef Q_OS_WIN
QString name = localpath.section("\\", -1);
#else
QString name = localpath.section("/", -1);
#endif
QStringList fldrActionsLst;
qDebug() << name;
if (!fldrActionsLst.contains(name))
{
QAction *fldrAction = new QAction(app->emptyFldrIcon,name, app);
fldrAction->setProperty("path", localpath);
connect(fldrAction, SIGNAL(triggered()), app, SLOT(openLocalDir()));
app->addNewFolderInMenu(fldrAction);
fldrActionsLst<<name;
}
}
++it;
}
QApplication::restoreOverrideCursor();
app->pCloudWin->get_sync_page()->load();
this->hide();
app->showSync();
}
QString SuggestnsBaseWin::checkRemoteName(QString &entryName)
{
if (remoteFldrsNamesLst.contains(entryName) || newRemoteFldrsLst.contains(entryName))
{
int i = 1;
QString newName = entryName;
while (remoteFldrsNamesLst.contains(newName)|| newRemoteFldrsLst.contains(entryName))
{
newName = entryName + "(" + QString::number(i) + ")";
i++;
}
return newName;
}
else
return entryName;
}
QString SuggestnsBaseWin::getLocalName(QString &entryName)
{
int i = 0;
QString newName = entryName;
while(dfltDir->exists(newName))
{
i++;
newName = entryName.append("(" + QString::number(i) + ")");
}
return dfltDir->path().append("/" + newName);
}
void SuggestnsBaseWin::addNewRemoteFldr(QString &name)
{
this->remoteFldrsNamesLst.append(name);
}
QString SuggestnsBaseWin::getCurrLocalPath()
{
return this->currentLocal;
}
QString SuggestnsBaseWin::getCurrRemotePath()
{
return this->currentRemote;
}
int SuggestnsBaseWin::getCurrType()
{
return this->currentType;
}
QTreeWidgetItem* SuggestnsBaseWin::getCurrItem()
{
return ui->treeWidget->currentItem();
}
void SuggestnsBaseWin::setChangeItem(bool b)
{
this->isChangingItem = b;
}