2 Copyright (c) 2014-2015 Montel Laurent <montel@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "storageservicetreewidget.h"
22 #include "pimcommon/storageserviceabstract.h"
23 #include "pimcommon/storageservicechecknamedialog.h"
24 #include "storageservicemanagerglobalconfig.h"
25 #include "pimcommon/storageserviceprogressmanager.h"
26 #include "storageservicemanager_debug.h"
29 #include <QInputDialog>
30 #include <KLocalizedString>
31 #include <KMessageBox>
35 #include <QHeaderView>
37 #include <KSharedConfig>
39 #include <QFontDatabase>
40 #include <QFileDialog>
42 StorageServiceTreeWidget::StorageServiceTreeWidget(PimCommon::StorageServiceAbstract
*storageService
, QWidget
*parent
)
43 : PimCommon::StorageServiceTreeWidget(storageService
, parent
),
46 mCapabilities
= mStorageService
->capabilities();
47 //Single selection for the moment
48 setSelectionMode(QAbstractItemView::SingleSelection
);
49 connect(this, &StorageServiceTreeWidget::fileDoubleClicked
, this, &StorageServiceTreeWidget::slotFileDoubleClicked
);
53 StorageServiceTreeWidget::~StorageServiceTreeWidget()
55 qCDebug(STORAGESERVICEMANAGER_LOG
) << " StorageServiceTreeWidget::~StorageServiceTreeWidget()";
59 void StorageServiceTreeWidget::changeEvent(QEvent
*event
)
61 if (event
->type() == QEvent::PaletteChange
) {
62 generalPaletteChanged();
63 } else if (event
->type() == QEvent::FontChange
) {
66 QTreeWidget::changeEvent(event
);
68 void StorageServiceTreeWidget::writeConfig()
70 KConfigGroup
grp(KSharedConfig::openConfig(), "StorageServiceTreeWidget");
71 grp
.writeEntry(mStorageService
->storageServiceName(), header()->saveState());
74 void StorageServiceTreeWidget::readConfig()
76 KConfigGroup
grp(KSharedConfig::openConfig(), "StorageServiceTreeWidget");
77 header()->restoreState(grp
.readEntry(mStorageService
->storageServiceName(), QByteArray()));
80 void StorageServiceTreeWidget::generalPaletteChanged()
82 const QPalette palette
= viewport()->palette();
83 QColor color
= palette
.text().color();
88 void StorageServiceTreeWidget::generalFontChanged()
90 setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
93 void StorageServiceTreeWidget::setIsInitialized()
97 Q_EMIT
listFileWasInitialized();
101 void StorageServiceTreeWidget::createMenuActions(QMenu
*menu
)
104 createUpAction(menu
);
105 const PimCommon::StorageServiceTreeWidget::ItemType type
= StorageServiceTreeWidget::itemTypeSelected();
106 if (type
!= StorageServiceTreeWidget::UnKnown
) {
107 if (type
== StorageServiceTreeWidget::File
) {
108 if (mCapabilities
& PimCommon::StorageServiceAbstract::MoveFileCapability
) {
109 menu
->addAction(QIcon::fromTheme(QStringLiteral("edit-cut")), i18n("Cut"), this, SLOT(slotCutFile()));
111 if (mCapabilities
& PimCommon::StorageServiceAbstract::CopyFileCapability
) {
112 menu
->addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy"), this, SLOT(slotCopyFile()));
114 QAction
*act
= new QAction(menu
);
115 act
->setSeparator(true);
116 menu
->addAction(act
);
117 if (mCapabilities
& PimCommon::StorageServiceAbstract::RenameFileCapabilitity
) {
118 menu
->addAction(i18n("Rename File..."), this, SLOT(slotRenameFile()));
120 if (mCapabilities
& PimCommon::StorageServiceAbstract::ShareLinkCapability
) {
121 menu
->addAction(i18n("Share File"), this, SLOT(slotShareFile()));
123 act
= new QAction(menu
);
124 act
->setSeparator(true);
125 menu
->addAction(act
);
126 if (mCapabilities
& PimCommon::StorageServiceAbstract::DownloadFileCapability
) {
127 menu
->addAction(QIcon::fromTheme(QStringLiteral("download")), i18n("Download File"), this, SIGNAL(downloadFile()));
129 act
= new QAction(menu
);
130 act
->setSeparator(true);
131 menu
->addAction(act
);
132 if (mCapabilities
& PimCommon::StorageServiceAbstract::DeleteFileCapability
) {
133 menu
->addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Delete File"), this, SLOT(slotDeleteFile()));
135 } else if (type
== StorageServiceTreeWidget::Folder
) {
136 if (mCapabilities
& PimCommon::StorageServiceAbstract::MoveFolderCapability
) {
137 menu
->addAction(QIcon::fromTheme(QStringLiteral("edit-cut")), i18n("Cut"), this, SLOT(slotCutFolder()));
139 if (mCapabilities
& PimCommon::StorageServiceAbstract::CopyFolderCapability
) {
140 menu
->addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy"), this, SLOT(slotCopyFolder()));
142 QAction
*act
= new QAction(menu
);
143 act
->setSeparator(true);
144 menu
->addAction(act
);
145 if (mCapabilities
& PimCommon::StorageServiceAbstract::RenameFolderCapability
) {
146 menu
->addAction(i18n("Rename Folder..."), this, SLOT(slotRenameFolder()));
148 act
= new QAction(menu
);
149 act
->setSeparator(true);
150 menu
->addAction(act
);
151 if (mCapabilities
& PimCommon::StorageServiceAbstract::DeleteFolderCapability
) {
152 menu
->addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Delete Folder"), this, SLOT(slotDeleteFolder()));
156 QAction
*act
= new QAction(menu
);
157 act
->setSeparator(true);
158 menu
->addAction(act
);
159 if (mCapabilities
& PimCommon::StorageServiceAbstract::UploadFileCapability
) {
160 menu
->addAction(i18n("Upload File..."), this, SIGNAL(uploadFile()));
162 act
= new QAction(menu
);
163 act
->setSeparator(true);
164 menu
->addAction(act
);
165 if (mCapabilities
& PimCommon::StorageServiceAbstract::CreateFolderCapability
) {
166 menu
->addAction(QIcon::fromTheme(QStringLiteral("folder-new")), i18n("Create Folder..."), this, SLOT(slotCreateFolder()));
169 act
= new QAction(menu
);
170 act
->setSeparator(true);
171 menu
->addAction(act
);
173 if (mCopyItem
.moveItem
) {
174 if (mCopyItem
.type
== FileType
) {
175 if (mCapabilities
& PimCommon::StorageServiceAbstract::MoveFileCapability
) {
176 menu
->addAction(QIcon::fromTheme(QStringLiteral("edit-paste")), i18n("Paste"), this, SLOT(slotMoveFile()));
178 } else if (mCopyItem
.type
== FolderType
) {
179 if (mCapabilities
& PimCommon::StorageServiceAbstract::MoveFolderCapability
) {
180 menu
->addAction(QIcon::fromTheme(QStringLiteral("edit-paste")), i18n("Paste"), this, SLOT(slotMoveFolder()));
184 if (mCopyItem
.type
== FileType
) {
185 if (mCapabilities
& PimCommon::StorageServiceAbstract::CopyFileCapability
) {
186 menu
->addAction(QIcon::fromTheme(QStringLiteral("edit-paste")), i18n("Paste"), this, SLOT(slotPasteFile()));
188 } else if (mCopyItem
.type
== FolderType
) {
189 if (mCapabilities
& PimCommon::StorageServiceAbstract::CopyFolderCapability
) {
190 menu
->addAction(QIcon::fromTheme(QStringLiteral("edit-paste")), i18n("Paste"), this, SLOT(slotPasteFolder()));
194 if ((type
== StorageServiceTreeWidget::File
) || (type
== StorageServiceTreeWidget::Folder
)) {
195 act
= new QAction(menu
);
196 act
->setSeparator(true);
197 menu
->addAction(act
);
198 createPropertiesAction(menu
);
201 menu
->addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Refresh"), this, SLOT(refreshList()));
205 void StorageServiceTreeWidget::slotMoveFolder()
207 mStorageService
->moveFolder(mCopyItem
.identifier
, mCurrentFolder
);
210 void StorageServiceTreeWidget::slotMoveFile()
212 mStorageService
->moveFile(mCopyItem
.identifier
, mCurrentFolder
);
215 void StorageServiceTreeWidget::slotPasteFolder()
217 mStorageService
->copyFolder(mCopyItem
.identifier
, mCurrentFolder
);
220 void StorageServiceTreeWidget::slotPasteFile()
222 mStorageService
->copyFile(mCopyItem
.identifier
, mCurrentFolder
);
225 void StorageServiceTreeWidget::renameItem()
227 switch (itemTypeSelected()) {
228 case StorageServiceTreeWidget::Folder
:
231 case StorageServiceTreeWidget::File
:
239 void StorageServiceTreeWidget::slotRenameFolder()
241 const QString oldFolderName
= itemIdentifierSelected();
242 const QString name
= currentItem()->text(0);
243 const QString folder
= QInputDialog::getText(this, i18n("Rename Folder Name"), i18n("Folder:"), QLineEdit::Normal
, name
);
244 if (!folder
.isEmpty()) {
245 if (name
!= folder
) {
246 if (!checkName(folder
)) {
249 mStorageService
->renameFolder(oldFolderName
, folder
);
254 void StorageServiceTreeWidget::slotRenameFile()
256 const QString oldFileName
= itemIdentifierSelected();
257 const QString name
= currentItem()->text(0);
258 const QString filename
= QInputDialog::getText(this, i18n("Rename Filename"), i18n("Filename:"), QLineEdit::Normal
, name
);
259 if (!filename
.trimmed().isEmpty()) {
260 if (name
!= filename
) {
261 if (!checkName(filename
)) {
264 mStorageService
->renameFile(oldFileName
, filename
);
269 bool StorageServiceTreeWidget::checkName(const QString
&name
)
271 const QRegExp disallowedSymbols
= QRegExp(mStorageService
->disallowedSymbols());
272 if (!disallowedSymbols
.isEmpty()) {
273 if (name
.contains(disallowedSymbols
)) {
274 KMessageBox::error(this, i18n("The following characters aren't allowed by %1:\n%2", mStorageService
->storageServiceName(), mStorageService
->disallowedSymbolsStr()), i18n("Create folder"));
278 if (name
== QLatin1String(".") || name
== QLatin1String("..")) {
279 KMessageBox::error(this, i18n("You cannot name a folder or file . or .."), i18n("Create Folder"));
285 void StorageServiceTreeWidget::slotCreateFolder()
287 const QString folder
= QInputDialog::getText(this, i18nc("@title:window", "Create Folder"), i18n("Folder:"));
288 if (!folder
.trimmed().isEmpty()) {
289 if (!checkName(folder
)) {
292 qCDebug(STORAGESERVICEMANAGER_LOG
) << " mCurrentFolder" << mCurrentFolder
;
293 mStorageService
->createFolder(folder
, mCurrentFolder
);
297 void StorageServiceTreeWidget::deleteItem()
299 switch (itemTypeSelected()) {
300 case StorageServiceTreeWidget::Folder
:
303 case StorageServiceTreeWidget::File
:
311 void StorageServiceTreeWidget::deleteFolder()
313 const QString folder
= itemIdentifierSelected();
314 const QString name
= currentItem()->text(0);
315 if (KMessageBox::Yes
== KMessageBox::questionYesNo(this, i18n("Are you sure that you want to delete \"%1\"?", name
))) {
316 if (!folder
.isEmpty()) {
317 mStorageService
->deleteFolder(folder
);
322 void StorageServiceTreeWidget::deleteFile()
324 const QString filename
= itemIdentifierSelected();
325 const QString name
= currentItem()->text(0);
326 if (KMessageBox::Yes
== KMessageBox::questionYesNo(this, i18n("Are you sure that you want to delete \"%1\"?", name
))) {
327 if (!filename
.isEmpty()) {
328 mStorageService
->deleteFile(filename
);
333 void StorageServiceTreeWidget::slotDeleteFolder()
335 if (itemTypeSelected() == StorageServiceTreeWidget::Folder
) {
340 void StorageServiceTreeWidget::slotDeleteFile()
342 if (itemTypeSelected() == StorageServiceTreeWidget::File
) {
347 void StorageServiceTreeWidget::slotShareFile()
349 if (itemTypeSelected() == StorageServiceTreeWidget::File
) {
350 const QString filename
= itemIdentifierSelected();
351 if (!filename
.isEmpty()) {
352 const QString rootShareFile
= mStorageService
->fileShareRoot(itemInformationSelected());
353 mStorageService
->shareLink(rootShareFile
, filename
);
358 void StorageServiceTreeWidget::slotDownloadFile()
360 if (itemTypeSelected() == StorageServiceTreeWidget::File
) {
361 const QString filename
= currentItem()->text(0);
362 if (!filename
.isEmpty()) {
363 QString destination
= StorageServiceManagerGlobalConfig::self()->downloadDirectory();
364 if (destination
.isEmpty()) {
365 destination
= QFileDialog::getExistingDirectory(this, QString());
366 if (destination
.isEmpty()) {
370 QFileInfo
fileInfo(destination
+ QLatin1Char('/') + filename
);
371 if (fileInfo
.exists()) {
372 if (KMessageBox::No
== KMessageBox::questionYesNo(this, i18n("Filename already exists. Do you want to overwrite it?"), i18n("Overwrite file"))) {
376 const QString fileId
= mStorageService
->fileIdentifier(itemInformationSelected());
377 PimCommon::StorageServiceProgressManager::self()->addProgress(mStorageService
, PimCommon::StorageServiceProgressManager::DownLoad
);
378 mStorageService
->downloadFile(filename
, fileId
, destination
);
383 bool StorageServiceTreeWidget::uploadFileToService()
385 const QString filename
= QFileDialog::getOpenFileName(this);
386 if (!filename
.isEmpty()) {
387 const QRegExp disallowedSymbols
= QRegExp(mStorageService
->disallowedSymbols());
388 const qlonglong maximumLimit
= mStorageService
->maximumUploadFileSize();
389 qCDebug(STORAGESERVICEMANAGER_LOG
) << " maximumLimit" << maximumLimit
;
390 QFileInfo
info(filename
);
391 if (maximumLimit
> 0 && (info
.size() > maximumLimit
)) {
392 KMessageBox::error(this, i18n("File size (%1) is larger than limit (%2)", KFormat().formatByteSize(info
.size(), 1), KFormat().formatByteSize(maximumLimit
, 1)));
395 if (filename
== QLatin1String(".") || filename
== QLatin1String("..")) {
396 KMessageBox::error(this, i18n("File names \".\" and \"..\" are forbidden."));
399 QString newName
= info
.fileName();
400 if (!disallowedSymbols
.isEmpty()) {
401 if (newName
.contains(disallowedSymbols
)) {
402 QPointer
<PimCommon::StorageServiceCheckNameDialog
> dlg
= new PimCommon::StorageServiceCheckNameDialog(this);
403 dlg
->setOldName(newName
);
404 dlg
->setDisallowedSymbols(disallowedSymbols
);
405 dlg
->setDisallowedSymbolsStr(mStorageService
->disallowedSymbolsStr());
407 newName
= dlg
->newName();
415 PimCommon::StorageServiceProgressManager::self()->addProgress(mStorageService
, PimCommon::StorageServiceProgressManager::Upload
);
416 mStorageService
->uploadFile(filename
, newName
, mCurrentFolder
);
423 void StorageServiceTreeWidget::canDownloadFile()
425 if (itemTypeSelected() == StorageServiceTreeWidget::File
) {
426 Q_EMIT
downloadFile();
428 KMessageBox::error(this, i18n("Please select a file to download."),
429 i18nc("@title:window", "Download File"));
433 void StorageServiceTreeWidget::paintEvent(QPaintEvent
*event
)
436 PimCommon::StorageServiceTreeWidget::paintEvent(event
);
438 QPainter
p(viewport());
440 QFont font
= p
.font();
441 font
.setItalic(true);
444 if (!mTextColor
.isValid()) {
445 generalPaletteChanged();
447 p
.setPen(mTextColor
);
448 p
.drawText(QRect(0, 0, width(), height()), Qt::AlignCenter
, i18n("Storage service not initialized."));
452 void StorageServiceTreeWidget::slotCutFile()
454 mCopyItem
.moveItem
= true;
455 mCopyItem
.type
= FileType
;
456 mCopyItem
.identifier
= itemIdentifierSelected();
459 void StorageServiceTreeWidget::slotCutFolder()
461 mCopyItem
.moveItem
= true;
462 mCopyItem
.type
= FolderType
;
463 mCopyItem
.identifier
= itemIdentifierSelected();
466 void StorageServiceTreeWidget::slotCopyFile()
468 mCopyItem
.moveItem
= false;
469 mCopyItem
.type
= FileType
;
470 mCopyItem
.identifier
= itemIdentifierSelected();
473 void StorageServiceTreeWidget::slotCopyFolder()
475 mCopyItem
.moveItem
= false;
476 mCopyItem
.type
= FolderType
;
477 mCopyItem
.identifier
= itemIdentifierSelected();
480 void StorageServiceTreeWidget::slotFileDoubleClicked()
482 if (mCapabilities
& PimCommon::StorageServiceAbstract::DownloadFileCapability
) {
483 if (KMessageBox::Yes
== KMessageBox::questionYesNo(this, i18n("Do you want to download this file?"),
484 i18nc("@title:window", "Download File"))) {
485 Q_EMIT
downloadFile();
490 bool StorageServiceTreeWidget::listFolderWasLoaded() const
495 void StorageServiceTreeWidget::logout()
497 mInitialized
= false;
499 mStorageService
->logout();