File indexing completed on 2024-04-28 04:58:28

0001 #include "konqsidebar_oldtreemodule.h"
0002 #include <kdesktopfile.h>
0003 #include "konq_sidebartree.h"
0004 #include <QVBoxLayout>
0005 #include <kstandarddirs.h>
0006 #include <KLocalizedString>
0007 #include <kconfig.h>
0008 #include <kpluginfactory.h>
0009 #include <kinputdialog.h>
0010 #include <kiconloader.h>
0011 #include <knameandurlinputdialog.h>
0012 #include <k3listviewsearchline.h>
0013 
0014 #include <QAction>
0015 #include <QClipboard>
0016 #include <QApplication>
0017 #include <QStandardPaths>
0018 
0019 KonqSidebarOldTreeModule::KonqSidebarOldTreeModule(const KComponentData &componentData, QWidget *parent,
0020         const QString &desktopName_, const KConfigGroup &configGroup)
0021     : KonqSidebarModule(componentData, parent, configGroup)
0022 {
0023     const ModuleType virt = configGroup.readEntry("X-KDE-TreeModule", QString()) == "Virtual" ? VIRT_Folder : VIRT_Link;
0024     QString path;
0025     if (virt == VIRT_Folder) {
0026         path = configGroup.readEntry("X-KDE-RelURL", QString());
0027     } else {
0028         // The whole idea of using the same desktop file for the module
0029         // and for the toplevel item is broken. When renaming the toplevel item,
0030         // the module isn't renamed until the next konqueror restart (!).
0031         // We probably want to get rid of toplevel items when there's only one?
0032         path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "konqsidebartng/entries/" + desktopName_); // ### this breaks global/local merging!
0033     }
0034 
0035     widget = new QWidget(parent);
0036     QVBoxLayout *widgetVBoxLayout = new QVBoxLayout(widget);
0037     widgetVBoxLayout->setContentsMargins(0, 0, 0, 0);
0038     // TODO use QVBoxLayout
0039 
0040     if (configGroup.readEntry("X-KDE-SearchableTreeModule", false)) {
0041         QWidget *searchLine = new QWidget(widget);
0042         QVBoxLayout *searchLineVBoxLayout = new QVBoxLayout(searchLine);
0043         searchLineVBoxLayout->setContentsMargins(0, 0, 0, 0);
0044         widgetVBoxLayout->addWidget(searchLine);
0045         tree = new KonqSidebarTree(this, widget, virt, path);
0046         new K3ListViewSearchLineWidget(tree, searchLine);
0047     } else {
0048         tree = new KonqSidebarTree(this, widget, virt, path);
0049     }
0050 
0051     connect(tree, SIGNAL(openUrlRequest(QUrl,KParts::OpenUrlArguments,BrowserArguments)),
0052             this, SIGNAL(openUrlRequest(QUrl,KParts::OpenUrlArguments,BrowserArguments)));
0053 
0054     connect(tree, SIGNAL(createNewWindow(QUrl,KParts::OpenUrlArguments,BrowserArguments)),
0055             this, SIGNAL(createNewWindow(QUrl,KParts::OpenUrlArguments,BrowserArguments)));
0056 
0057     connect(tree, SIGNAL(copy()),
0058             this, SLOT(copy()));
0059     connect(tree, SIGNAL(cut()),
0060             this, SLOT(cut()));
0061     connect(tree, SIGNAL(paste()),
0062             this, SLOT(pasteToSelection()));
0063 }
0064 
0065 KonqSidebarOldTreeModule::~KonqSidebarOldTreeModule() {}
0066 
0067 QWidget *KonqSidebarOldTreeModule::getWidget()
0068 {
0069     return widget;
0070 }
0071 
0072 void KonqSidebarOldTreeModule::handleURL(const QUrl &url)
0073 {
0074     emit started(0);
0075     tree->followURL(url);
0076     emit completed();
0077 }
0078 
0079 void KonqSidebarOldTreeModule::cut()
0080 {
0081     QMimeData *mimeData = new QMimeData;
0082     if (static_cast<KonqSidebarTreeItem *>(tree->selectedItem())->populateMimeData(mimeData, true)) {
0083         QApplication::clipboard()->setMimeData(mimeData);
0084     } else {
0085         delete mimeData;
0086     }
0087 }
0088 
0089 void KonqSidebarOldTreeModule::copy()
0090 {
0091     qCDebug(SIDEBAR_LOG);
0092     QMimeData *mimeData = new QMimeData;
0093     if (static_cast<KonqSidebarTreeItem *>(tree->selectedItem())->populateMimeData(mimeData, false)) {
0094         qCDebug(SIDEBAR_LOG) << "setting" << mimeData->formats();
0095         QApplication::clipboard()->setMimeData(mimeData);
0096     } else {
0097         delete mimeData;
0098     }
0099 }
0100 
0101 void KonqSidebarOldTreeModule::paste()
0102 {
0103     // Not implemented. Would be for pasting into the toplevel.
0104     qCDebug(SIDEBAR_LOG) << "not implemented. Didn't think it would be called - tell me (David Faure)";
0105 }
0106 
0107 void KonqSidebarOldTreeModule::pasteToSelection()
0108 {
0109     if (tree->currentItem()) {
0110         tree->currentItem()->paste();
0111     }
0112 }
0113 
0114 class KonqSidebarTreePlugin : public KonqSidebarPlugin
0115 {
0116 public:
0117     KonqSidebarTreePlugin(QObject *parent, const QVariantList &args)
0118         : KonqSidebarPlugin(parent, args) {}
0119     virtual ~KonqSidebarTreePlugin() {}
0120 
0121     virtual KonqSidebarModule *createModule(const KComponentData &componentData, QWidget *parent,
0122                                             const KConfigGroup &configGroup,
0123                                             const QString &desktopname,
0124                                             const QVariant &unused)
0125     {
0126         Q_UNUSED(unused);
0127         return new KonqSidebarOldTreeModule(componentData, parent, desktopname, configGroup);
0128     }
0129 
0130     virtual QList<QAction *> addNewActions(QObject *parent,
0131                                            const QList<KConfigGroup> &existingModules,
0132                                            const QVariant &unused)
0133     {
0134         Q_UNUSED(unused);
0135 
0136         QStringList existingTreeModules;
0137         for (const KConfigGroup &cfg: existingModules) {
0138             existingTreeModules.append(cfg.readEntry("X-KDE-TreeModule", QString()));
0139         }
0140 
0141         QList<QAction *> actions;
0142         const QStringList list = KGlobal::dirs()->findAllResources("data",
0143                                  "konqsidebartng/dirtree/*.desktop",
0144                                  KStandardDirs::NoDuplicates);
0145         for (const QString &desktopFile: list) {
0146             KDesktopFile df(desktopFile);
0147             const KConfigGroup desktopGroup = df.desktopGroup();
0148             const bool hasUrl = !(desktopGroup.readEntry("X-KDE-Default-URL", QString()).isEmpty());
0149             const QString treeModule = desktopGroup.readEntry("X-KDE-TreeModule", QString());
0150 
0151             // Assumption: modules without a default URL, don't use URLs at all,
0152             // and therefore are "unique" (no point in having two bookmarks modules
0153             // or two history modules). Modules with URLs can be added multiple times.
0154             if (hasUrl || !existingTreeModules.contains(treeModule)) {
0155                 const QString name = df.readName();
0156 
0157                 QAction *action = new QAction(parent);
0158                 //action->setText(i18nc("@action:inmenu Add folder sidebar module", "Folder"));
0159                 action->setText(name);
0160                 action->setData(desktopFile);
0161                 action->setIcon(QIcon::fromTheme(df.readIcon()));
0162                 actions.append(action);
0163             }
0164         }
0165         return actions;
0166     }
0167 
0168     virtual QString templateNameForNewModule(const QVariant &actionData,
0169             const QVariant &unused) const
0170     {
0171         Q_UNUSED(unused);
0172         // Example: /full/path/to/bookmarks_module.desktop -> bookmarks%1.desktop
0173         QString str = actionData.toString();
0174         str = str.mid(str.lastIndexOf('/') + 1);
0175         str.replace(".desktop", "%1.desktop");
0176         str.remove("_module");
0177         return str;
0178     }
0179 
0180     virtual bool createNewModule(const QVariant &actionData, KConfigGroup &configGroup,
0181                                  QWidget *parentWidget,
0182                                  const QVariant &unused)
0183     {
0184         Q_UNUSED(unused);
0185         const KDesktopFile df(actionData.toString());
0186         const KConfigGroup desktopGroup = df.desktopGroup();
0187         QUrl url = desktopGroup.readEntry("X-KDE-Default-URL");
0188         KNameAndUrlInputDialog dlg(i18nc("@label", "Name:"), i18nc("@label", "Path or URL:"), QUrl(), parentWidget);
0189         dlg.setCaption(i18nc("@title:window", "Add folder sidebar module"));
0190         dlg.setSuggestedName(df.readName());
0191         if (!dlg.exec()) {
0192             return false;
0193         }
0194 
0195         configGroup.writeEntry("Type", "Link");
0196         configGroup.writeEntry("Icon", df.readIcon());
0197         configGroup.writeEntry("Name", dlg.name());
0198         configGroup.writeEntry("Open", false);
0199         configGroup.writePathEntry("URL", dlg.url().url());
0200         configGroup.writeEntry("X-KDE-KonqSidebarModule", "konqsidebar_tree");
0201         configGroup.writeEntry("X-KDE-TreeModule", desktopGroup.readEntry("X-KDE-TreeModule"));
0202         configGroup.writeEntry("X-KDE-TreeModule-ShowHidden", desktopGroup.readEntry("X-KDE-TreeModule-ShowHidden"));
0203         return true;
0204     }
0205 };
0206 
0207 K_PLUGIN_FACTORY(KonqSidebarTreePluginFactory, registerPlugin<KonqSidebarTreePlugin>();)
0208 K_EXPORT_PLUGIN(KonqSidebarTreePluginFactory())
0209 #include "konqsidebar_oldtreemodule.moc"