SVN_SILENT made messages (.desktop file)
[kdepim.git] / storageservicemanager / storageservicetreewidget.cpp
blobe7a36678c4287738abca40fb428965c265f27fee
1 /*
2 Copyright (c) 2014 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
17 02110-1301, USA.
21 #include "storageservicetreewidget.h"
22 #include "storageservice/storageserviceabstract.h"
23 #include "storageservice/dialog/storageservicechecknamedialog.h"
24 #include "storageservicemanagerglobalconfig.h"
25 #include "storageservice/storageserviceprogressmanager.h"
27 #include <KMenu>
28 #include <KInputDialog>
29 #include <KLocalizedString>
30 #include <KFileDialog>
31 #include <KGlobalSettings>
32 #include <KMessageBox>
33 #include <KLocale>
35 #include <QPainter>
36 #include <QHeaderView>
37 #include <QTimer>
38 #include <QPointer>
40 StorageServiceTreeWidget::StorageServiceTreeWidget(PimCommon::StorageServiceAbstract *storageService, QWidget *parent)
41 : PimCommon::StorageServiceTreeWidget(storageService, parent),
42 mInitialized(false)
44 mCapabilities = mStorageService->capabilities();
45 //Single selection for the moment
46 setSelectionMode(QAbstractItemView::SingleSelection);
47 connect(this, SIGNAL(fileDoubleClicked()), this, SLOT(slotFileDoubleClicked()));
48 connect( KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()), this, SLOT(slotGeneralFontChanged()));
49 connect( KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), this, SLOT(slotGeneralPaletteChanged()));
50 readConfig();
53 StorageServiceTreeWidget::~StorageServiceTreeWidget()
55 qDebug()<<" StorageServiceTreeWidget::~StorageServiceTreeWidget()";
56 writeConfig();
59 void StorageServiceTreeWidget::writeConfig()
61 KConfigGroup grp( KGlobal::config(), "StorageServiceTreeWidget" );
62 grp.writeEntry(mStorageService->storageServiceName(), header()->saveState());
65 void StorageServiceTreeWidget::readConfig()
67 KConfigGroup grp( KGlobal::config(), "StorageServiceTreeWidget" );
68 header()->restoreState( grp.readEntry( mStorageService->storageServiceName(), QByteArray() ) );
71 void StorageServiceTreeWidget::slotGeneralPaletteChanged()
73 const QPalette palette = viewport()->palette();
74 QColor color = palette.text().color();
75 color.setAlpha( 128 );
76 mTextColor = color;
79 void StorageServiceTreeWidget::slotGeneralFontChanged()
81 setFont( KGlobalSettings::generalFont() );
84 void StorageServiceTreeWidget::setIsInitialized()
86 if (!mInitialized) {
87 mInitialized = true;
88 Q_EMIT listFileWasInitialized();
92 void StorageServiceTreeWidget::createMenuActions(KMenu *menu)
94 if (mInitialized) {
95 createUpAction(menu);
96 const PimCommon::StorageServiceTreeWidget::ItemType type = StorageServiceTreeWidget::itemTypeSelected();
97 if (type != StorageServiceTreeWidget::UnKnown) {
98 if (type == StorageServiceTreeWidget::File) {
99 if (mCapabilities & PimCommon::StorageServiceAbstract::MoveFileCapability)
100 menu->addAction(KIcon(QLatin1String("edit-cut")), i18n("Cut"), this, SLOT(slotCutFile()));
101 if (mCapabilities & PimCommon::StorageServiceAbstract::CopyFileCapability)
102 menu->addAction(KIcon(QLatin1String("edit-copy")), i18n("Copy"), this, SLOT(slotCopyFile()));
103 QAction *act = new QAction(menu);
104 act->setSeparator(true);
105 menu->addAction(act);
106 if (mCapabilities & PimCommon::StorageServiceAbstract::RenameFileCapabilitity)
107 menu->addAction(i18n("Rename File..."), this, SLOT(slotRenameFile()));
108 if (mCapabilities & PimCommon::StorageServiceAbstract::ShareLinkCapability)
109 menu->addAction(i18n("Share File"), this, SLOT(slotShareFile()));
110 act = new QAction(menu);
111 act->setSeparator(true);
112 menu->addAction(act);
113 if (mCapabilities & PimCommon::StorageServiceAbstract::DownloadFileCapability)
114 menu->addAction(KIcon(QLatin1String("download")), i18n("Download File"), this, SIGNAL(downloadFile()));
115 act = new QAction(menu);
116 act->setSeparator(true);
117 menu->addAction(act);
118 if (mCapabilities & PimCommon::StorageServiceAbstract::DeleteFileCapability)
119 menu->addAction(KIcon(QLatin1String("edit-delete")), i18n("Delete File"), this, SLOT(slotDeleteFile()));
120 } else if (type == StorageServiceTreeWidget::Folder) {
121 if (mCapabilities & PimCommon::StorageServiceAbstract::MoveFolderCapability)
122 menu->addAction(KIcon(QLatin1String("edit-cut")), i18n("Cut"), this, SLOT(slotCutFolder()));
123 if (mCapabilities & PimCommon::StorageServiceAbstract::CopyFolderCapability)
124 menu->addAction(KIcon(QLatin1String("edit-copy")), i18n("Copy"), this, SLOT(slotCopyFolder()));
125 QAction *act = new QAction(menu);
126 act->setSeparator(true);
127 menu->addAction(act);
128 if (mCapabilities & PimCommon::StorageServiceAbstract::RenameFolderCapability)
129 menu->addAction(i18n("Rename Folder..."), this, SLOT(slotRenameFolder()));
130 act = new QAction(menu);
131 act->setSeparator(true);
132 menu->addAction(act);
133 if (mCapabilities & PimCommon::StorageServiceAbstract::DeleteFolderCapability)
134 menu->addAction(KIcon(QLatin1String("edit-delete")), i18n("Delete Folder"), this, SLOT(slotDeleteFolder()));
137 QAction *act = new QAction(menu);
138 act->setSeparator(true);
139 menu->addAction(act);
140 if (mCapabilities & PimCommon::StorageServiceAbstract::UploadFileCapability)
141 menu->addAction(i18n("Upload File..."), this, SIGNAL(uploadFile()));
142 act = new QAction(menu);
143 act->setSeparator(true);
144 menu->addAction(act);
145 if (mCapabilities & PimCommon::StorageServiceAbstract::CreateFolderCapability)
146 menu->addAction(KIcon(QLatin1String("folder-new")), i18n("Create Folder..."), this, SLOT(slotCreateFolder()));
148 act = new QAction(menu);
149 act->setSeparator(true);
150 menu->addAction(act);
152 if (mCopyItem.moveItem) {
153 if (mCopyItem.type == FileType) {
154 if (mCapabilities & PimCommon::StorageServiceAbstract::MoveFileCapability) {
155 menu->addAction(KIcon(QLatin1String("edit-paste")), i18n("Paste"), this, SLOT(slotMoveFile()));
157 } else if (mCopyItem.type == FolderType) {
158 if (mCapabilities & PimCommon::StorageServiceAbstract::MoveFolderCapability) {
159 menu->addAction(KIcon(QLatin1String("edit-paste")), i18n("Paste"), this, SLOT(slotMoveFolder()));
162 } else {
163 if (mCopyItem.type == FileType) {
164 if (mCapabilities & PimCommon::StorageServiceAbstract::CopyFileCapability) {
165 menu->addAction(KIcon(QLatin1String("edit-paste")), i18n("Paste"), this, SLOT(slotPasteFile()));
167 } else if (mCopyItem.type == FolderType) {
168 if (mCapabilities & PimCommon::StorageServiceAbstract::CopyFolderCapability) {
169 menu->addAction(KIcon(QLatin1String("edit-paste")), i18n("Paste"), this, SLOT(slotPasteFolder()));
173 if ((type == StorageServiceTreeWidget::File) || (type == StorageServiceTreeWidget::Folder)) {
174 act = new QAction(menu);
175 act->setSeparator(true);
176 menu->addAction(act);
177 createPropertiesAction(menu);
179 } else {
180 menu->addAction(KIcon(QLatin1String("view-refresh")), i18n("Refresh"), this, SLOT(refreshList()));
184 void StorageServiceTreeWidget::slotMoveFolder()
186 mStorageService->moveFolder(mCopyItem.identifier, mCurrentFolder);
189 void StorageServiceTreeWidget::slotMoveFile()
191 mStorageService->moveFile(mCopyItem.identifier, mCurrentFolder);
194 void StorageServiceTreeWidget::slotPasteFolder()
196 mStorageService->copyFolder(mCopyItem.identifier, mCurrentFolder);
199 void StorageServiceTreeWidget::slotPasteFile()
201 mStorageService->copyFile(mCopyItem.identifier, mCurrentFolder);
204 void StorageServiceTreeWidget::renameItem()
206 switch(itemTypeSelected()) {
207 case StorageServiceTreeWidget::Folder:
208 slotRenameFolder();
209 break;
210 case StorageServiceTreeWidget::File:
211 slotRenameFile();
212 break;
213 default:
214 break;
218 void StorageServiceTreeWidget::slotRenameFolder()
220 const QString oldFolderName = itemIdentifierSelected();
221 const QString name = currentItem()->text(0);
222 const QString folder = KInputDialog::getText(i18n("Rename Folder Name"), i18n("Folder:"), name);
223 if (!folder.isEmpty()) {
224 if (name != folder) {
225 if (!checkName(folder)) {
226 return;
228 mStorageService->renameFolder(oldFolderName, folder);
233 void StorageServiceTreeWidget::slotRenameFile()
235 const QString oldFileName = itemIdentifierSelected();
236 const QString name = currentItem()->text(0);
237 const QString filename = KInputDialog::getText(i18n("Rename Filename"), i18n("Filename:"), name);
238 if (!filename.isEmpty()) {
239 if (name != filename) {
240 if (!checkName(filename)) {
241 return;
243 mStorageService->renameFile(oldFileName, filename);
248 bool StorageServiceTreeWidget::checkName(const QString &name)
250 const QRegExp disallowedSymbols = mStorageService->disallowedSymbols();
251 if (!disallowedSymbols.isEmpty()) {
252 if (name.contains(disallowedSymbols)) {
253 KMessageBox::error(this, i18n("The following characters aren't allowed by %1:\n%2", mStorageService->storageServiceName(), mStorageService->disallowedSymbolsStr()), i18n("Create folder"));
254 return false;
257 if (name == QLatin1String(".") || name == QLatin1String("..")) {
258 KMessageBox::error(this, i18n("You cannot name a folder or file . or .."), i18n("Create Folder"));
259 return false;
261 return true;
264 void StorageServiceTreeWidget::slotCreateFolder()
266 const QString folder = KInputDialog::getText(i18n("Folder Name"), i18n("Folder:"));
267 if (!folder.isEmpty()) {
268 if (!checkName(folder))
269 return;
270 qDebug()<<" mCurrentFolder" <<mCurrentFolder;
271 mStorageService->createFolder(folder, mCurrentFolder);
275 void StorageServiceTreeWidget::deleteItem()
277 switch(itemTypeSelected()) {
278 case StorageServiceTreeWidget::Folder:
279 deleteFolder();
280 break;
281 case StorageServiceTreeWidget::File:
282 deleteFile();
283 break;
284 default:
285 break;
289 void StorageServiceTreeWidget::deleteFolder()
291 const QString folder = itemIdentifierSelected();
292 const QString name = currentItem()->text(0);
293 if (KMessageBox::Yes == KMessageBox::questionYesNo(this, i18n("Are you sure that you want to delete \"%1\"?", name))) {
294 if (!folder.isEmpty()) {
295 mStorageService->deleteFolder(folder);
300 void StorageServiceTreeWidget::deleteFile()
302 const QString filename = itemIdentifierSelected();
303 const QString name = currentItem()->text(0);
304 if (KMessageBox::Yes == KMessageBox::questionYesNo(this, i18n("Are you sure that you want to delete \"%1\"?", name) )) {
305 if (!filename.isEmpty()) {
306 mStorageService->deleteFile(filename);
311 void StorageServiceTreeWidget::slotDeleteFolder()
313 if (itemTypeSelected() == StorageServiceTreeWidget::Folder) {
314 deleteFolder();
318 void StorageServiceTreeWidget::slotDeleteFile()
320 if (itemTypeSelected() == StorageServiceTreeWidget::File) {
321 deleteFile();
325 void StorageServiceTreeWidget::slotShareFile()
327 if (itemTypeSelected() == StorageServiceTreeWidget::File) {
328 const QString filename = itemIdentifierSelected();
329 if (!filename.isEmpty()) {
330 const QString rootShareFile = mStorageService->fileShareRoot(itemInformationSelected());
331 mStorageService->shareLink(rootShareFile, filename);
336 void StorageServiceTreeWidget::slotDownloadFile()
338 if (itemTypeSelected() == StorageServiceTreeWidget::File) {
339 const QString filename = currentItem()->text(0);
340 if (!filename.isEmpty()) {
341 QString destination = StorageServiceManagerGlobalConfig::self()->downloadDirectory();
342 if (destination.isEmpty()) {
343 destination = KFileDialog::getExistingDirectory(KUrl(), this);
344 if (destination.isEmpty())
345 return;
347 QFileInfo fileInfo(destination + QLatin1Char('/') + filename);
348 if (fileInfo.exists()) {
349 if (KMessageBox::No == KMessageBox::questionYesNo(this, i18n("Filename already exists. Do you want to overwrite it?"), i18n("Overwrite file"))) {
350 return;
353 const QString fileId = mStorageService->fileIdentifier(itemInformationSelected());
354 PimCommon::StorageServiceProgressManager::self()->addProgress(mStorageService, PimCommon::StorageServiceProgressManager::DownLoad);
355 mStorageService->downloadFile(filename, fileId, destination);
360 bool StorageServiceTreeWidget::uploadFileToService()
362 const QString filename = KFileDialog::getOpenFileName(KUrl(), QLatin1String("*"), this);
363 if (!filename.isEmpty()) {
364 const QRegExp disallowedSymbols = mStorageService->disallowedSymbols();
365 const qlonglong maximumLimit = mStorageService->maximumUploadFileSize();
366 qDebug()<<" maximumLimit"<<maximumLimit;
367 QFileInfo info(filename);
368 if (maximumLimit > 0 && (info.size() > maximumLimit)) {
369 KMessageBox::error(this, i18n("File size (%1) is larger than limit (%2)", KGlobal::locale()->formatByteSize(info.size(),1), KGlobal::locale()->formatByteSize(maximumLimit,1)));
370 return false;
372 if (filename == QLatin1String(".") || filename == QLatin1String("..")) {
373 KMessageBox::error(this, i18n("You are trying to use unauthorized characters."));
374 return false;
376 QString newName = info.fileName();
377 if (!disallowedSymbols.isEmpty()) {
378 if (newName.contains(disallowedSymbols)) {
379 QPointer<PimCommon::StorageServiceCheckNameDialog> dlg = new PimCommon::StorageServiceCheckNameDialog(this);
380 dlg->setOldName(newName);
381 dlg->setDisallowedSymbols(disallowedSymbols);
382 dlg->setDisallowedSymbolsStr(mStorageService->disallowedSymbolsStr());
383 if (dlg->exec()) {
384 newName = dlg->newName();
385 delete dlg;
386 } else {
387 delete dlg;
388 return false;
392 PimCommon::StorageServiceProgressManager::self()->addProgress(mStorageService, PimCommon::StorageServiceProgressManager::Upload);
393 mStorageService->uploadFile(filename, newName, mCurrentFolder);
394 return true;
395 } else {
396 return false;
400 void StorageServiceTreeWidget::canDownloadFile()
402 if (itemTypeSelected() == StorageServiceTreeWidget::File) {
403 Q_EMIT downloadFile();
404 } else {
405 KMessageBox::error(this, i18n("Please select a file to download."), i18n("Download File"));
409 void StorageServiceTreeWidget::paintEvent( QPaintEvent *event )
411 if ( mInitialized ) {
412 PimCommon::StorageServiceTreeWidget::paintEvent(event);
413 } else {
414 QPainter p( viewport() );
416 QFont font = p.font();
417 font.setItalic( true );
418 p.setFont( font );
420 if (!mTextColor.isValid()) {
421 slotGeneralPaletteChanged();
423 p.setPen( mTextColor );
424 p.drawText( QRect( 0, 0, width(), height() ), Qt::AlignCenter, i18n("Storage service not initialized.") );
428 void StorageServiceTreeWidget::slotCutFile()
430 mCopyItem.moveItem = true;
431 mCopyItem.type = FileType;
432 mCopyItem.identifier = itemIdentifierSelected();
435 void StorageServiceTreeWidget::slotCutFolder()
437 mCopyItem.moveItem = true;
438 mCopyItem.type = FolderType;
439 mCopyItem.identifier = itemIdentifierSelected();
442 void StorageServiceTreeWidget::slotCopyFile()
444 mCopyItem.moveItem = false;
445 mCopyItem.type = FileType;
446 mCopyItem.identifier = itemIdentifierSelected();
449 void StorageServiceTreeWidget::slotCopyFolder()
451 mCopyItem.moveItem = false;
452 mCopyItem.type = FolderType;
453 mCopyItem.identifier = itemIdentifierSelected();
456 void StorageServiceTreeWidget::slotFileDoubleClicked()
458 if (mCapabilities & PimCommon::StorageServiceAbstract::DownloadFileCapability) {
459 if (KMessageBox::Yes == KMessageBox::questionYesNo(this, i18n("Do you want to download this file?"), i18n("Download File"))) {
460 Q_EMIT downloadFile();
465 bool StorageServiceTreeWidget::listFolderWasLoaded() const
467 return mInitialized;
470 void StorageServiceTreeWidget::logout()
472 mInitialized = false;
473 clear();
474 mStorageService->logout();