• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkonq

konq_copytomenu.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002 
00003    Copyright 2008 David Faure <faure@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU Library General Public License as published
00007    by the Free Software Foundation; either version 2 of the License or
00008    ( at your option ) version 3 or, at the discretion of KDE e.V.
00009    ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "konq_copytomenu.h"
00023 #include "konq_copytomenu_p.h"
00024 #include "konq_operations.h"
00025 #include <kaction.h>
00026 #include <kdebug.h>
00027 #include <kicon.h>
00028 #include <kfiledialog.h>
00029 #include <klocale.h>
00030 #include <kmenu.h>
00031 #include <kstringhandler.h>
00032 #include <QDir>
00033 
00034 KonqCopyToMenuPrivate::KonqCopyToMenuPrivate()
00035     : m_urls(), m_readOnly(false)
00036 {
00037 }
00038 
00040 
00041 KonqCopyToMenu::KonqCopyToMenu()
00042     : d(new KonqCopyToMenuPrivate)
00043 {
00044 
00045 }
00046 
00047 KonqCopyToMenu::~KonqCopyToMenu()
00048 {
00049     delete d;
00050 }
00051 
00052 void KonqCopyToMenu::setItems(const KFileItemList& items)
00053 {
00054     // For now we lose all the information except for the urls
00055     // But this API is useful in case KIO can make use of this information later
00056     // (e.g. to avoid stat'ing the source urls)
00057     Q_FOREACH(const KFileItem& item, items)
00058         d->m_urls.append(item.url());
00059 }
00060 
00061 void KonqCopyToMenu::setUrls(const KUrl::List& urls)
00062 {
00063     d->m_urls = urls;
00064 }
00065 
00066 void KonqCopyToMenu::setReadOnly(bool ro)
00067 {
00068     d->m_readOnly = ro;
00069 }
00070 
00071 void KonqCopyToMenu::addActionsTo(QMenu* menu)
00072 {
00073     KMenu* mainCopyMenu = new KonqCopyToMainMenu(menu, d, Copy);
00074     mainCopyMenu->setTitle(i18nc("@title:menu", "Copy To"));
00075     mainCopyMenu->menuAction()->setObjectName("copyTo_submenu"); // for the unittest
00076     menu->addMenu(mainCopyMenu);
00077 
00078     if (!d->m_readOnly) {
00079         KMenu* mainMoveMenu = new KonqCopyToMainMenu(menu, d, Move);
00080         mainMoveMenu->setTitle(i18nc("@title:menu", "Move To"));
00081         mainMoveMenu->menuAction()->setObjectName("moveTo_submenu"); // for the unittest
00082         menu->addMenu(mainMoveMenu);
00083     }
00084 }
00085 
00087 
00088 KonqCopyToMainMenu::KonqCopyToMainMenu(QMenu* parent, KonqCopyToMenuPrivate* _d, MenuType menuType)
00089     : KMenu(parent), m_menuType(menuType),
00090       m_actionGroup(static_cast<QWidget *>(0)),
00091       d(_d),
00092       m_recentDirsGroup(KGlobal::config(), m_menuType == Copy ? "kuick-copy" : "kuick-move")
00093 {
00094     connect(this, SIGNAL(aboutToShow()), SLOT(slotAboutToShow()));
00095     connect(&m_actionGroup, SIGNAL(triggered(QAction*)), SLOT(slotTriggered(QAction*)));
00096 }
00097 
00098 void KonqCopyToMainMenu::slotAboutToShow()
00099 {
00100     clear();
00101     KonqCopyToDirectoryMenu* subMenu;
00102     // Home Folder
00103     subMenu = new KonqCopyToDirectoryMenu(this, this, QDir::homePath());
00104     subMenu->setTitle(i18nc("@title:menu", "Home Folder"));
00105     subMenu->setIcon(KIcon("go-home"));
00106     addMenu(subMenu);
00107 
00108     // Root Folder
00109     // TODO on Windows: one submenu per drive? (Or even a Drives submenu with the drives in it?)
00110     subMenu = new KonqCopyToDirectoryMenu(this, this, QDir::rootPath());
00111     subMenu->setTitle(i18nc("@title:menu", "Root Folder"));
00112     subMenu->setIcon(KIcon("folder-red"));
00113     addMenu(subMenu);
00114 
00115     // Browse... action, shows a KFileDialog
00116     KAction* browseAction = new KAction(i18nc("@title:menu in Copy To or Move To submenu", "Browse..."), this);
00117     connect(browseAction, SIGNAL(triggered()), this, SLOT(slotBrowse()));
00118     addAction(browseAction);
00119 
00120     addSeparator(); // looks like Qt4 handles removing it automatically if it's last in the menu, nice.
00121 
00122     // Recent Destinations
00123     const QStringList recentDirs = m_recentDirsGroup.readPathEntry("Paths", QStringList());
00124     Q_FOREACH(const QString& recentDir, recentDirs) {
00125         const KUrl url(recentDir);
00126         const QString text = KStringHandler::csqueeze(url.pathOrUrl(), 60); // shorten very long paths (#61386)
00127         KAction* act = new KAction(text, this);
00128         act->setData(url);
00129         m_actionGroup.addAction(act);
00130         addAction(act);
00131     }
00132 }
00133 
00134 void KonqCopyToMainMenu::slotBrowse()
00135 {
00136     const KUrl dest = KFileDialog::getExistingDirectoryUrl(KUrl("kfiledialog:///copyto"), this);
00137     if (!dest.isEmpty()) {
00138         copyOrMoveTo(dest);
00139     }
00140 }
00141 
00142 void KonqCopyToMainMenu::slotTriggered(QAction* action)
00143 {
00144     const KUrl url = action->data().value<KUrl>();
00145     Q_ASSERT(!url.isEmpty());
00146     copyOrMoveTo(url);
00147 }
00148 
00149 void KonqCopyToMainMenu::copyOrMoveTo(const KUrl& dest)
00150 {
00151     // Insert into the recent destinations list
00152     QStringList recentDirs = m_recentDirsGroup.readPathEntry("Paths", QStringList());
00153     const QString niceDest = dest.pathOrUrl();
00154     if (!recentDirs.contains(niceDest)) { // don't change position if already there, moving stuff is bad usability
00155         recentDirs.prepend(niceDest);
00156         while (recentDirs.size() > 10) { // hardcoded max size
00157             recentDirs.removeLast();
00158         }
00159         m_recentDirsGroup.writePathEntry("Paths", recentDirs);
00160     }
00161 
00162     // And now let's do the copy or move -- with undo/redo support.
00163     KonqOperations::copy(this, m_menuType == Copy ? KonqOperations::COPY : KonqOperations::MOVE,
00164                          d->m_urls, dest);
00165 }
00166 
00168 
00169 KonqCopyToDirectoryMenu::KonqCopyToDirectoryMenu(QMenu* parent, KonqCopyToMainMenu* mainMenu, const QString& path)
00170     : KMenu(parent), m_mainMenu(mainMenu), m_path(path)
00171 {
00172     connect(this, SIGNAL(aboutToShow()), SLOT(slotAboutToShow()));
00173 }
00174 
00175 void KonqCopyToDirectoryMenu::slotAboutToShow()
00176 {
00177     clear();
00178     KAction* act = new KAction(m_mainMenu->menuType() == Copy
00179                                ? i18nc("@title:menu", "Copy Here")
00180                                : i18nc("@title:menu", "Move Here"), this);
00181     act->setData(KUrl(m_path));
00182     act->setEnabled(QFileInfo(m_path).isWritable());
00183     m_mainMenu->actionGroup().addAction(act);
00184     addAction(act);
00185 
00186     addSeparator(); // looks like Qt4 handles removing it automatically if it's last in the menu, nice.
00187 
00188     // List directory
00189     // All we need is sub folder names, their permissions, their icon.
00190     // KDirLister or KIO::listDir would fetch much more info, and would be async,
00191     // and we only care about local directories so we use QDir directly.
00192     QDir dir(m_path);
00193     const QStringList entries = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::LocaleAware);
00194     KMimeType::Ptr dirMime = KMimeType::mimeType("inode/directory");
00195     Q_FOREACH(const QString& subDir, entries) {
00196         const QString subPath = m_path + '/' + subDir;
00197         KonqCopyToDirectoryMenu* subMenu = new KonqCopyToDirectoryMenu(this, m_mainMenu, subPath);
00198         subMenu->setTitle(subDir);
00199         const QString iconName = dirMime->iconName(KUrl(subPath));
00200         subMenu->setIcon(KIcon(iconName));
00201         if (QFileInfo(subPath).isSymLink()) { // I hope this isn't too slow...
00202             QFont font = subMenu->menuAction()->font();
00203             font.setItalic(true);
00204             subMenu->menuAction()->setFont(font);
00205         }
00206         addMenu(subMenu);
00207     }
00208 }

libkonq

Skip menu "libkonq"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal