Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / keditbookmarks / main.cpp
blob758eb62b0becca91c849b9f69799e9905c3863a1
1 // -*- indent-tabs-mode:nil -*-
2 // vim: set ts=4 sts=4 sw=4 et:
3 /* This file is part of the KDE project
4 Copyright (C) 2000 David Faure <faure@kde.org>
5 Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License version 2 or at your option version 3 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
23 #include "toplevel.h"
24 #include "importers.h"
26 #include <klocale.h>
27 #include <kdebug.h>
28 #include <kdeversion.h>
29 #include <kstandarddirs.h>
31 #include <kcmdlineargs.h>
32 #include <kaboutdata.h>
33 #include <kuniqueapplication.h>
35 #include <kmessagebox.h>
36 #include <kwindowsystem.h>
37 #include <unistd.h>
39 #include <kbookmarkmanager.h>
40 #include <kbookmarkexporter.h>
41 #include <toplevel_interface.h>
43 // TODO - make this register() or something like that and move dialog into main
44 static bool askUser(const QString& filename, bool &readonly) {
46 QString requestedName("keditbookmarks");
47 QString interfaceName = "org.kde.keditbookmarks";
48 QString appId = interfaceName + '-' +QString().setNum(getpid());
50 QDBusConnection dbus = QDBusConnection::sessionBus();
51 QDBusReply<QStringList> reply = dbus.interface()->registeredServiceNames();
52 if ( !reply.isValid() )
53 return true;
54 const QStringList allServices = reply;
55 for ( QStringList::const_iterator it = allServices.begin(), end = allServices.end() ; it != end ; ++it ) {
56 const QString service = *it;
57 if ( service.startsWith( interfaceName ) && service != appId ) {
58 org::kde::keditbookmarks keditbookmarks(service,"/keditbookmarks", dbus);
59 QDBusReply<QString> bookmarks = keditbookmarks.bookmarkFilename();
60 QString name;
61 if( bookmarks.isValid())
62 name = bookmarks;
63 if( name == filename)
65 int ret = KMessageBox::warningYesNo(0,
66 i18n("Another instance of %1 is already running, do you really "
67 "want to open another instance or continue work in the same instance?\n"
68 "Please note that, unfortunately, duplicate views are read-only.", KGlobal::caption()),
69 i18n("Warning"),
70 KGuiItem(i18n("Run Another")), /* yes */
71 KGuiItem(i18n("Continue in Same")) /* no */);
72 if (ret == KMessageBox::No) {
73 QDBusInterface keditinterface(service, "/keditbookmarks/MainWindow_1");
74 //TODO fix me
75 QDBusReply<qlonglong> value = keditinterface.call(QDBus::NoBlock, "winId");
76 qlonglong id = 0;
77 if( value.isValid())
78 id = value;
79 //kDebug()<<" id !!!!!!!!!!!!!!!!!!! :"<<id;
80 KWindowSystem::activateWindow((WId)id);
81 return false;
82 } else if (ret == KMessageBox::Yes) {
83 readonly = true;
88 return true;
91 #include <kactioncollection.h>
93 extern "C" KDE_EXPORT int kdemain(int argc, char **argv) {
94 KAboutData aboutData("keditbookmarks", 0, ki18n("Bookmark Editor"), KDE_VERSION_STRING,
95 ki18n("Bookmark Organizer and Editor"),
96 KAboutData::License_GPL,
97 ki18n("Copyright 2000-2007, KDE developers") );
98 aboutData.addAuthor(ki18n("David Faure"), ki18n("Initial author"), "faure@kde.org");
99 aboutData.addAuthor(ki18n("Alexander Kellett"), ki18n("Author"), "lypanov@kde.org");
101 KCmdLineArgs::init(argc, argv, &aboutData);
102 KCmdLineArgs::addStdCmdLineOptions();
104 KCmdLineOptions options;
105 options.add("importmoz <filename>", ki18n("Import bookmarks from a file in Mozilla format"));
106 options.add("importns <filename>", ki18n("Import bookmarks from a file in Netscape (4.x and earlier) format"));
107 options.add("importie <filename>", ki18n("Import bookmarks from a file in Internet Explorer's Favorites format"));
108 options.add("importopera <filename>", ki18n("Import bookmarks from a file in Opera format"));
109 options.add("exportmoz <filename>", ki18n("Export bookmarks to a file in Mozilla format"));
110 options.add("exportns <filename>", ki18n("Export bookmarks to a file in Netscape (4.x and earlier) format"));
111 options.add("exporthtml <filename>", ki18n("Export bookmarks to a file in a printable HTML format"));
112 options.add("exportie <filename>", ki18n("Export bookmarks to a file in Internet Explorer's Favorites format"));
113 options.add("exportopera <filename>", ki18n("Export bookmarks to a file in Opera format"));
114 options.add("address <address>", ki18n("Open at the given position in the bookmarks file"));
115 options.add("customcaption <caption>", ki18n("Set the user readable caption for example \"Konsole\""));
116 options.add("nobrowser", ki18n("Hide all browser related functions"));
117 options.add("dbusObjectName <name>", ki18n("A unique name that represents this bookmark collection, usually the kinstance name.\n"
118 "This should be \"konqueror\" for the konqueror bookmarks, \"kfile\" for KFileDialog bookmarks, etc.\n"
119 "The final DBus object path is /KBookmarkManager/dbusObjectName"));
120 options.add("+[file]", ki18n("File to edit"));
121 KCmdLineArgs::addCmdLineOptions(options);
123 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
124 bool isGui = !(args->isSet("exportmoz") || args->isSet("exportns") || args->isSet("exporthtml")
125 || args->isSet("exportie") || args->isSet("exportopera")
126 || args->isSet("importmoz") || args->isSet("importns")
127 || args->isSet("importie") || args->isSet("importopera"));
129 bool browser = args->isSet("browser");
131 //KApplication::disableAutoDcopRegistration();
132 KApplication app(isGui);
134 bool gotFilenameArg = (args->count() == 1);
136 QString filename = gotFilenameArg
137 ? args->arg(0)
138 : KStandardDirs::locateLocal("data", QLatin1String("konqueror/bookmarks.xml"));
140 if (!isGui) {
141 CurrentMgr::self()->createManager(filename, QString());
142 CurrentMgr::ExportType exportType = CurrentMgr::MozillaExport; // uumm.. can i just set it to -1 ?
143 int got = 0;
144 const char *arg, *arg2 = 0, *importType = 0;
145 if (arg = "exportmoz", args->isSet(arg)) { exportType = CurrentMgr::MozillaExport; arg2 = arg; got++; }
146 if (arg = "exportns", args->isSet(arg)) { exportType = CurrentMgr::NetscapeExport; arg2 = arg; got++; }
147 if (arg = "exporthtml", args->isSet(arg)) { exportType = CurrentMgr::HTMLExport; arg2 = arg; got++; }
148 if (arg = "exportie", args->isSet(arg)) { exportType = CurrentMgr::IEExport; arg2 = arg; got++; }
149 if (arg = "exportopera", args->isSet(arg)) { exportType = CurrentMgr::OperaExport; arg2 = arg; got++; }
150 if (arg = "importmoz", args->isSet(arg)) { importType = "Moz"; arg2 = arg; got++; }
151 if (arg = "importns", args->isSet(arg)) { importType = "NS"; arg2 = arg; got++; }
152 if (arg = "importie", args->isSet(arg)) { importType = "IE"; arg2 = arg; got++; }
153 if (arg = "importopera", args->isSet(arg)) { importType = "Opera"; arg2 = arg; got++; }
154 if (!importType && arg2) {
155 Q_ASSERT(arg2);
156 // TODO - maybe an xbel export???
157 if (got > 1) // got == 0 isn't possible as !isGui is dependant on "export.*"
158 KCmdLineArgs::usage(I18N_NOOP("You may only specify a single --export option."));
159 QString path = args->getOption(arg2);
160 CurrentMgr::self()->doExport(exportType, path);
161 } else if (importType) {
162 if (got > 1) // got == 0 isn't possible as !isGui is dependant on "import.*"
163 KCmdLineArgs::usage(I18N_NOOP("You may only specify a single --import option."));
164 QString path = args->getOption(arg2);
165 ImportCommand *importer = ImportCommand::importerFactory(importType);
166 importer->import(path, true);
167 importer->execute();
168 CurrentMgr::self()->managerSave();
169 CurrentMgr::self()->notifyManagers();
171 return 0; // error flag on exit?, 1?
174 QString address = args->isSet("address")
175 ? args->getOption("address")
176 : QString("/0");
178 QString caption = args->isSet("customcaption")
179 ? args->getOption("customcaption")
180 : QString();
182 QString dbusObjectName;
183 if(args->isSet("dbusObjectName"))
185 dbusObjectName = args->getOption("dbusObjectName");
187 else
189 if(gotFilenameArg)
190 dbusObjectName = QString();
191 else
192 dbusObjectName = "konqueror";
195 args->clear();
197 bool readonly = false; // passed by ref
199 if (askUser((gotFilenameArg ? filename : QString()), readonly)) {
200 KEBApp *toplevel = new KEBApp(filename, readonly, address, browser, caption, dbusObjectName);
201 toplevel->show();
202 return app.exec();
205 return 0;