1 /*
2 * Copyright (C) by Klaas Freitag <freitag@owncloud.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include <QtGui>
16 #include <QtWidgets>
17
18 #include "protocolwidget.h"
19 #include "configfile.h"
20 #include "syncresult.h"
21 #include "logger.h"
22 #include "theme.h"
23 #include "folderman.h"
24 #include "syncfileitem.h"
25 #include "folder.h"
26 #include "openfilemanager.h"
27 #include "activityitemdelegate.h"
28 #include "guiutility.h"
29 #include "accountstate.h"
30
31 #include "ui_protocolwidget.h"
32
33 #include <climits>
34
35 Q_DECLARE_METATYPE(OCC::ProtocolItem::ExtraData)
36
37 namespace OCC {
38
timeString(QDateTime dt,QLocale::FormatType format)39 QString ProtocolItem::timeString(QDateTime dt, QLocale::FormatType format)
40 {
41 const QLocale loc = QLocale::system();
42 QString dtFormat = loc.dateTimeFormat(format);
43 static const QRegExp re("(HH|H|hh|h):mm(?!:s)");
44 dtFormat.replace(re, "\\1:mm:ss");
45 return loc.toString(dt, dtFormat);
46 }
47
extraData(const QTreeWidgetItem * item)48 ProtocolItem::ExtraData ProtocolItem::extraData(const QTreeWidgetItem *item)
49 {
50 return item->data(0, Qt::UserRole).value<ExtraData>();
51 }
52
setExtraData(QTreeWidgetItem * item,const ExtraData & data)53 void ProtocolItem::setExtraData(QTreeWidgetItem *item, const ExtraData &data)
54 {
55 item->setData(0, Qt::UserRole, QVariant::fromValue(data));
56 }
57
create(const QString & folderName,const SyncFileItem & item)58 ProtocolItem *ProtocolItem::create(const QString &folderName, const SyncFileItem &item)
59 {
60 auto folder = FolderMan::instance()->folder(folderName);
61
62 QStringList columns;
63 QDateTime timestamp = QDateTime::currentDateTime();
64 const QString timeStr = timeString(timestamp);
65 const QString longTimeStr = timeString(timestamp, QLocale::LongFormat);
66
67 columns << timeStr;
68 columns << Utility::fileNameForGuiUse(item._originalFile);
69 columns << (folder ? folder->shortGuiLocalPath() : QDir::toNativeSeparators(folderName));
70
71 // If the error string is set, it's prefered because it is a useful user message.
72 QString message = item._errorString;
73 if (message.isEmpty()) {
74 message = item._messageString;
75 }
76 if (message.isEmpty()) {
77 message = Progress::asResultString(item);
78 }
79 columns << message;
80
81 QIcon icon;
82 if (item._status == SyncFileItem::NormalError
83 || item._status == SyncFileItem::FatalError
84 || item._status == SyncFileItem::DetailError
85 || item._status == SyncFileItem::BlacklistedError) {
86 icon = Theme::instance()->syncStateIcon(SyncResult::Error);
87 } else if (Progress::isWarningKind(item._status)) {
88 icon = Theme::instance()->syncStateIcon(SyncResult::Problem);
89 }
90
91 if (ProgressInfo::isSizeDependent(item)) {
92 columns << Utility::octetsToString(item._size);
93 }
94
95 ProtocolItem *twitem = new ProtocolItem(columns);
96 // Warning: The data and tooltips on the columns define an implicit
97 // interface and can only be changed with care.
98 twitem->setData(0, Qt::SizeHintRole, QSize(0, ActivityItemDelegate::rowHeight()));
99 twitem->setIcon(0, icon);
100 twitem->setToolTip(0, longTimeStr);
101 twitem->setToolTip(1, item.destination());
102 twitem->setToolTip(3, message);
103 ProtocolItem::ExtraData data;
104 data.timestamp = timestamp;
105 data.path = item.destination();
106 data.folderName = folderName;
107 data.status = item._status;
108 data.size = item._size;
109 data.direction = item._direction;
110 ProtocolItem::setExtraData(twitem, data);
111 return twitem;
112 }
113
syncJournalRecord(QTreeWidgetItem * item)114 SyncJournalFileRecord ProtocolItem::syncJournalRecord(QTreeWidgetItem *item)
115 {
116 SyncJournalFileRecord rec;
117 auto f = folder(item);
118 if (!f)
119 return rec;
120 f->journalDb()->getFileRecord(extraData(item).path, &rec);
121 return rec;
122 }
123
folder(QTreeWidgetItem * item)124 Folder *ProtocolItem::folder(QTreeWidgetItem *item)
125 {
126 return FolderMan::instance()->folder(extraData(item).folderName);
127 }
128
openContextMenu(QPoint globalPos,QTreeWidgetItem * item,QWidget * parent)129 void ProtocolItem::openContextMenu(QPoint globalPos, QTreeWidgetItem *item, QWidget *parent)
130 {
131 auto f = folder(item);
132 if (!f)
133 return;
134 AccountPtr account = f->accountState()->account();
135 auto rec = syncJournalRecord(item);
136 // rec might not be valid
137
138 auto menu = new QMenu(parent);
139
140 if (rec.isValid()) {
141 // "Open in Browser" action
142 auto openInBrowser = menu->addAction(ProtocolWidget::tr("Open in browser"));
143 QObject::connect(openInBrowser, &QAction::triggered, parent, [parent, account, rec]() {
144 fetchPrivateLinkUrl(account, rec._path, rec.legacyDeriveNumericFileId(), parent,
145 [parent](const QString &url) {
146 Utility::openBrowser(url, parent);
147 });
148 });
149 }
150
151 // More actions will be conditionally added to the context menu here later
152
153 if (menu->actions().isEmpty()) {
154 delete menu;
155 return;
156 }
157
158 menu->setAttribute(Qt::WA_DeleteOnClose);
159 menu->popup(globalPos);
160 }
161
operator <(const QTreeWidgetItem & other) const162 bool ProtocolItem::operator<(const QTreeWidgetItem &other) const
163 {
164 int column = treeWidget()->sortColumn();
165 if (column == 0) {
166 // Items with empty "File" column are larger than others,
167 // otherwise sort by time (this uses lexicographic ordering)
168 return std::forward_as_tuple(text(1).isEmpty(), extraData(this).timestamp)
169 < std::forward_as_tuple(other.text(1).isEmpty(), extraData(&other).timestamp);
170 } else if (column == 4) {
171 return extraData(this).size < extraData(&other).size;
172 }
173
174 return QTreeWidgetItem::operator<(other);
175 }
176
ProtocolWidget(QWidget * parent)177 ProtocolWidget::ProtocolWidget(QWidget *parent)
178 : QWidget(parent)
179 , _ui(new Ui::ProtocolWidget)
180 {
181 _ui->setupUi(this);
182
183 connect(ProgressDispatcher::instance(), &ProgressDispatcher::itemCompleted,
184 this, &ProtocolWidget::slotItemCompleted);
185
186 connect(_ui->_treeWidget, &QTreeWidget::itemActivated, this, &ProtocolWidget::slotOpenFile);
187
188 _ui->_treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
189 connect(_ui->_treeWidget, &QTreeWidget::customContextMenuRequested, this, &ProtocolWidget::slotItemContextMenu);
190
191 // Adjust copyToClipboard() when making changes here!
192 QStringList header;
193 header << tr("Time");
194 header << tr("File");
195 header << tr("Folder");
196 header << tr("Action");
197 header << tr("Size");
198
199 _ui->_treeWidget->setHeaderLabels(header);
200 int timestampColumnWidth =
201 _ui->_treeWidget->fontMetrics().boundingRect(ProtocolItem::timeString(QDateTime::currentDateTime())).width();
202 _ui->_treeWidget->setColumnWidth(0, timestampColumnWidth);
203 _ui->_treeWidget->setColumnWidth(1, 180);
204 _ui->_treeWidget->setColumnCount(5);
205 _ui->_treeWidget->setRootIsDecorated(false);
206 _ui->_treeWidget->setTextElideMode(Qt::ElideMiddle);
207 _ui->_treeWidget->header()->setObjectName("ActivityListHeader");
208 #if defined(Q_OS_MAC)
209 _ui->_treeWidget->setMinimumWidth(400);
210 #endif
211 _ui->_headerLabel->setText(tr("Local sync protocol"));
212
213 QPushButton *copyBtn = _ui->_dialogButtonBox->addButton(tr("Copy"), QDialogButtonBox::ActionRole);
214 copyBtn->setToolTip(tr("Copy the activity list to the clipboard."));
215 copyBtn->setEnabled(true);
216 connect(copyBtn, &QAbstractButton::clicked, this, &ProtocolWidget::copyToClipboard);
217 }
218
~ProtocolWidget()219 ProtocolWidget::~ProtocolWidget()
220 {
221 delete _ui;
222 }
223
showEvent(QShowEvent * ev)224 void ProtocolWidget::showEvent(QShowEvent *ev)
225 {
226 ConfigFile cfg;
227 cfg.restoreGeometryHeader(_ui->_treeWidget->header());
228
229 // Sorting by section was newly enabled. But if we restore the header
230 // from a state where sorting was disabled, both of these flags will be
231 // false and sorting will be impossible!
232 _ui->_treeWidget->header()->setSectionsClickable(true);
233 _ui->_treeWidget->header()->setSortIndicatorShown(true);
234
235 // Switch back to "by time" ordering
236 _ui->_treeWidget->sortByColumn(0, Qt::DescendingOrder);
237
238 QWidget::showEvent(ev);
239 }
240
hideEvent(QHideEvent * ev)241 void ProtocolWidget::hideEvent(QHideEvent *ev)
242 {
243 ConfigFile cfg;
244 cfg.saveGeometryHeader(_ui->_treeWidget->header());
245 QWidget::hideEvent(ev);
246 }
247
slotItemContextMenu(const QPoint & pos)248 void ProtocolWidget::slotItemContextMenu(const QPoint &pos)
249 {
250 auto item = _ui->_treeWidget->itemAt(pos);
251 if (!item)
252 return;
253 auto globalPos = _ui->_treeWidget->viewport()->mapToGlobal(pos);
254 ProtocolItem::openContextMenu(globalPos, item, this);
255 }
256
slotOpenFile(QTreeWidgetItem * item,int)257 void ProtocolWidget::slotOpenFile(QTreeWidgetItem *item, int)
258 {
259 QString fileName = item->text(1);
260 if (Folder *folder = ProtocolItem::folder(item)) {
261 // folder->path() always comes back with trailing path
262 QString fullPath = folder->path() + fileName;
263 if (QFile(fullPath).exists()) {
264 showInFileManager(fullPath);
265 }
266 }
267 }
268
slotItemCompleted(const QString & folder,const SyncFileItemPtr & item)269 void ProtocolWidget::slotItemCompleted(const QString &folder, const SyncFileItemPtr &item)
270 {
271 if (!item->showInProtocolTab())
272 return;
273 QTreeWidgetItem *line = ProtocolItem::create(folder, *item);
274 if (line) {
275 // Limit the number of items
276 int itemCnt = _ui->_treeWidget->topLevelItemCount();
277 while (itemCnt > 2000) {
278 delete _ui->_treeWidget->takeTopLevelItem(itemCnt - 1);
279 itemCnt--;
280 }
281 _ui->_treeWidget->insertTopLevelItem(0, line);
282 }
283 }
284
storeSyncActivity(QTextStream & ts)285 void ProtocolWidget::storeSyncActivity(QTextStream &ts)
286 {
287 int topLevelItems = _ui->_treeWidget->topLevelItemCount();
288
289 for (int i = 0; i < topLevelItems; i++) {
290 QTreeWidgetItem *child = _ui->_treeWidget->topLevelItem(i);
291 ts << right
292 // time stamp
293 << qSetFieldWidth(20)
294 << child->data(0, Qt::DisplayRole).toString()
295 // separator
296 << qSetFieldWidth(0) << ","
297
298 // file name
299 << qSetFieldWidth(64)
300 << child->data(1, Qt::DisplayRole).toString()
301 // separator
302 << qSetFieldWidth(0) << ","
303
304 // folder
305 << qSetFieldWidth(30)
306 << child->data(2, Qt::DisplayRole).toString()
307 // separator
308 << qSetFieldWidth(0) << ","
309
310 // action
311 << qSetFieldWidth(15)
312 << child->data(3, Qt::DisplayRole).toString()
313 // separator
314 << qSetFieldWidth(0) << ","
315
316 // size
317 << qSetFieldWidth(10)
318 << child->data(4, Qt::DisplayRole).toString()
319 << qSetFieldWidth(0)
320 << endl;
321 }
322 }
323
324 }
325