Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / keditbookmarks / main.cpp
blobef111be5a6e83f879c06d7a1dffde4e017a0d50e
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");
100 aboutData.setProgramIconName("bookmarks-organize");
102 KCmdLineArgs::init(argc, argv, &aboutData);
103 KCmdLineArgs::addStdCmdLineOptions();
105 KCmdLineOptions options;
106 options.add("importmoz <filename>", ki18n("Import bookmarks from a file in Mozilla format"));
107 options.add("importns <filename>", ki18n("Import bookmarks from a file in Netscape (4.x and earlier) format"));
108 options.add("importie <filename>", ki18n("Import bookmarks from a file in Internet Explorer's Favorites format"));
109 options.add("importopera <filename>", ki18n("Import bookmarks from a file in Opera format"));
110 options.add("exportmoz <filename>", ki18n("Export bookmarks to a file in Mozilla format"));
111 options.add("exportns <filename>", ki18n("Export bookmarks to a file in Netscape (4.x and earlier) format"));
112 options.add("exporthtml <filename>", ki18n("Export bookmarks to a file in a printable HTML format"));
113 options.add("exportie <filename>", ki18n("Export bookmarks to a file in Internet Explorer's Favorites format"));
114 options.add("exportopera <filename>", ki18n("Export bookmarks to a file in Opera format"));
115 options.add("address <address>", ki18n("Open at the given position in the bookmarks file"));
116 options.add("customcaption <caption>", ki18n("Set the user-readable caption, for example \"Konsole\""));
117 options.add("nobrowser", ki18n("Hide all browser related functions"));
118 options.add("dbusObjectName <name>", ki18n("A unique name that represents this bookmark collection, usually the kinstance name.\n"
119 "This should be \"konqueror\" for the Konqueror bookmarks, \"kfile\" for KFileDialog bookmarks, etc.\n"
120 "The final D-Bus object path is /KBookmarkManager/dbusObjectName"));
121 options.add("+[file]", ki18n("File to edit"));
122 KCmdLineArgs::addCmdLineOptions(options);
124 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
125 bool isGui = !(args->isSet("exportmoz") || args->isSet("exportns") || args->isSet("exporthtml")
126 || args->isSet("exportie") || args->isSet("exportopera")
127 || args->isSet("importmoz") || args->isSet("importns")
128 || args->isSet("importie") || args->isSet("importopera"));
130 bool browser = args->isSet("browser");
132 //KApplication::disableAutoDcopRegistration();
133 KApplication app(isGui);
135 bool gotFilenameArg = (args->count() == 1);
137 QString filename = gotFilenameArg
138 ? args->arg(0)
139 : KStandardDirs::locateLocal("data", QLatin1String("konqueror/bookmarks.xml"));
141 if (!isGui) {
142 CurrentMgr::self()->createManager(filename, QString());
143 CurrentMgr::ExportType exportType = CurrentMgr::MozillaExport; // uumm.. can i just set it to -1 ?
144 int got = 0;
145 const char *arg, *arg2 = 0, *importType = 0;
146 if (arg = "exportmoz", args->isSet(arg)) { exportType = CurrentMgr::MozillaExport; arg2 = arg; got++; }
147 if (arg = "exportns", args->isSet(arg)) { exportType = CurrentMgr::NetscapeExport; arg2 = arg; got++; }
148 if (arg = "exporthtml", args->isSet(arg)) { exportType = CurrentMgr::HTMLExport; arg2 = arg; got++; }
149 if (arg = "exportie", args->isSet(arg)) { exportType = CurrentMgr::IEExport; arg2 = arg; got++; }
150 if (arg = "exportopera", args->isSet(arg)) { exportType = CurrentMgr::OperaExport; arg2 = arg; got++; }
151 if (arg = "importmoz", args->isSet(arg)) { importType = "Moz"; arg2 = arg; got++; }
152 if (arg = "importns", args->isSet(arg)) { importType = "NS"; arg2 = arg; got++; }
153 if (arg = "importie", args->isSet(arg)) { importType = "IE"; arg2 = arg; got++; }
154 if (arg = "importopera", args->isSet(arg)) { importType = "Opera"; arg2 = arg; got++; }
155 if (!importType && arg2) {
156 Q_ASSERT(arg2);
157 // TODO - maybe an xbel export???
158 if (got > 1) // got == 0 isn't possible as !isGui is dependant on "export.*"
159 KCmdLineArgs::usage(I18N_NOOP("You may only specify a single --export option."));
160 QString path = args->getOption(arg2);
161 CurrentMgr::self()->doExport(exportType, path);
162 } else if (importType) {
163 if (got > 1) // got == 0 isn't possible as !isGui is dependant on "import.*"
164 KCmdLineArgs::usage(I18N_NOOP("You may only specify a single --import option."));
165 QString path = args->getOption(arg2);
166 ImportCommand *importer = ImportCommand::importerFactory(importType);
167 importer->import(path, true);
168 importer->execute();
169 CurrentMgr::self()->managerSave();
170 CurrentMgr::self()->notifyManagers();
172 return 0; // error flag on exit?, 1?
175 QString address = args->isSet("address")
176 ? args->getOption("address")
177 : QString("/0");
179 QString caption = args->isSet("customcaption")
180 ? args->getOption("customcaption")
181 : QString();
183 QString dbusObjectName;
184 if(args->isSet("dbusObjectName"))
186 dbusObjectName = args->getOption("dbusObjectName");
188 else
190 if(gotFilenameArg)
191 dbusObjectName = QString();
192 else
193 dbusObjectName = "konqueror";
196 args->clear();
198 bool readonly = false; // passed by ref
200 if (askUser((gotFilenameArg ? filename : QString()), readonly)) {
201 KEBApp *toplevel = new KEBApp(filename, readonly, address, browser, caption, dbusObjectName);
202 toplevel->show();
203 return app.exec();
206 return 0;