File indexing completed on 2022-11-29 20:24:10

0001 // -*- tab-width:4; indent-tabs-mode:t -*-
0002 /**
0003  * kbookmarkmerger.cpp - Copyright (C) 2005 Frerich Raabe <raabe@kde.org>
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  *
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0017  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0018  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0019  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0020  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0022  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0024  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025  */
0026 #include <KAboutData>
0027 
0028 #include <KBookmarkManager>
0029 
0030 #include "keditbookmarks_debug.h"
0031 
0032 #include <KLocalizedString>
0033 #include <QApplication>
0034 #include <QCommandLineParser>
0035 #include <QDir>
0036 #include <qdom.h>
0037 
0038 #include "keditbookmarks_version.h"
0039 
0040 int main(int argc, char **argv)
0041 {
0042     QApplication app(argc, argv);
0043 
0044     KLocalizedString::setApplicationDomain("keditbookmarks");
0045 
0046     KAboutData aboutData(QStringLiteral("kbookmarkmerger"),
0047                          i18n("KBookmarkMerger"),
0048                          QStringLiteral(KEDITBOOKMARKS_VERSION_STRING),
0049                          i18n("Merges bookmarks installed by 3rd parties into the user's bookmarks"),
0050                          KAboutLicense::BSDL,
0051                          i18n("Copyright © 2005 Frerich Raabe"));
0052     aboutData.addAuthor(i18n("Frerich Raabe"), i18n("Original author"), QStringLiteral("raabe@kde.org"));
0053 
0054     KAboutData::setApplicationData(aboutData);
0055 
0056     QCommandLineParser parser;
0057     parser.addPositionalArgument(QStringLiteral("directory"), i18n("Directory to scan for extra bookmarks"));
0058 
0059     aboutData.setupCommandLine(&parser);
0060     parser.process(app);
0061     aboutData.processCommandLine(&parser);
0062 
0063     if (parser.positionalArguments().count() != 1) {
0064         qCCritical(KEDITBOOKMARKS_LOG) << "No directory to scan for bookmarks specified.";
0065         return 1;
0066     }
0067 
0068     KBookmarkManager *konqBookmarks = KBookmarkManager::userBookmarksManager();
0069     QStringList mergedFiles;
0070     {
0071         KBookmarkGroup root = konqBookmarks->root();
0072         for (KBookmark bm = root.first(); !bm.isNull(); bm = root.next(bm)) {
0073             if (bm.isGroup()) {
0074                 continue;
0075             }
0076 
0077             QString mergedFrom = bm.metaDataItem(QStringLiteral("merged_from"));
0078             if (!mergedFrom.isNull()) {
0079                 mergedFiles << mergedFrom;
0080             }
0081         }
0082     }
0083 
0084     bool didMergeBookmark = false;
0085 
0086     QString extraBookmarksDirName = parser.positionalArguments().at(0);
0087     QDir extraBookmarksDir(extraBookmarksDirName, QStringLiteral("*.xml"));
0088     if (!extraBookmarksDir.isReadable()) {
0089         qCCritical(KEDITBOOKMARKS_LOG) << "Failed to read files in directory " << extraBookmarksDirName;
0090         return 1;
0091     }
0092 
0093     for (unsigned int i = 0; i < extraBookmarksDir.count(); ++i) {
0094         const QString fileName = extraBookmarksDir[i];
0095         if (mergedFiles.contains(fileName)) {
0096             continue;
0097         }
0098 
0099         const QString absPath = extraBookmarksDir.filePath(fileName);
0100         KBookmarkManager *mgr = KBookmarkManager::managerForFile(absPath, QString());
0101         KBookmarkGroup root = mgr->root();
0102         for (KBookmark bm = root.first(); !bm.isNull(); bm = root.next(bm)) {
0103             if (bm.isGroup()) {
0104                 continue;
0105             }
0106             bm.setMetaDataItem(QStringLiteral("merged_from"), fileName);
0107             konqBookmarks->root().addBookmark(bm);
0108             didMergeBookmark = true;
0109         }
0110     }
0111 
0112     if (didMergeBookmark) {
0113         konqBookmarks->emitChanged(konqBookmarks->root()); // calls save
0114         // see TODO in emitChanged... if it returns false, it would be nice to return 1
0115         // here.
0116     }
0117     return 0;
0118 }