File indexing completed on 2024-04-14 05:43: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 <QStandardPaths>
0037 #include <qdom.h>
0038 
0039 #include "keditbookmarks_version.h"
0040 
0041 int main(int argc, char **argv)
0042 {
0043     QApplication app(argc, argv);
0044 
0045     KLocalizedString::setApplicationDomain(QByteArrayLiteral("keditbookmarks"));
0046 
0047     KAboutData aboutData(QStringLiteral("kbookmarkmerger"),
0048                          i18n("KBookmarkMerger"),
0049                          QStringLiteral(KEDITBOOKMARKS_VERSION_STRING),
0050                          i18n("Merges bookmarks installed by 3rd parties into the user's bookmarks"),
0051                          KAboutLicense::BSDL,
0052                          i18n("Copyright © 2005 Frerich Raabe"));
0053     aboutData.addAuthor(i18n("Frerich Raabe"), i18n("Original author"), QStringLiteral("raabe@kde.org"));
0054 
0055     KAboutData::setApplicationData(aboutData);
0056 
0057     QCommandLineParser parser;
0058     parser.addPositionalArgument(QStringLiteral("directory"), i18n("Directory to scan for extra bookmarks"));
0059 
0060     aboutData.setupCommandLine(&parser);
0061     parser.process(app);
0062     aboutData.processCommandLine(&parser);
0063 
0064     if (parser.positionalArguments().count() != 1) {
0065         qCCritical(KEDITBOOKMARKS_LOG) << "No directory to scan for bookmarks specified.";
0066         return 1;
0067     }
0068 
0069     const QString bookmarksFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/konqueror/bookmarks.xml");
0070     KBookmarkManager konqBookmarks(bookmarksFile);
0071     QStringList mergedFiles;
0072     {
0073         KBookmarkGroup root = konqBookmarks.root();
0074         for (KBookmark bm = root.first(); !bm.isNull(); bm = root.next(bm)) {
0075             if (bm.isGroup()) {
0076                 continue;
0077             }
0078 
0079             QString mergedFrom = bm.metaDataItem(QStringLiteral("merged_from"));
0080             if (!mergedFrom.isNull()) {
0081                 mergedFiles << mergedFrom;
0082             }
0083         }
0084     }
0085 
0086     bool didMergeBookmark = false;
0087 
0088     QString extraBookmarksDirName = parser.positionalArguments().at(0);
0089     QDir extraBookmarksDir(extraBookmarksDirName, QStringLiteral("*.xml"));
0090     if (!extraBookmarksDir.isReadable()) {
0091         qCCritical(KEDITBOOKMARKS_LOG) << "Failed to read files in directory " << extraBookmarksDirName;
0092         return 1;
0093     }
0094 
0095     for (unsigned int i = 0; i < extraBookmarksDir.count(); ++i) {
0096         const QString fileName = extraBookmarksDir[i];
0097         if (mergedFiles.contains(fileName)) {
0098             continue;
0099         }
0100 
0101         const QString absPath = extraBookmarksDir.filePath(fileName);
0102         KBookmarkManager mgr(absPath);
0103         KBookmarkGroup root = mgr.root();
0104         for (KBookmark bm = root.first(); !bm.isNull(); bm = root.next(bm)) {
0105             if (bm.isGroup()) {
0106                 continue;
0107             }
0108             bm.setMetaDataItem(QStringLiteral("merged_from"), fileName);
0109             konqBookmarks.root().addBookmark(bm);
0110             didMergeBookmark = true;
0111         }
0112     }
0113 
0114     if (didMergeBookmark) {
0115         konqBookmarks.emitChanged(konqBookmarks.root()); // calls save
0116         // see TODO in emitChanged... if it returns false, it would be nice to return 1
0117         // here.
0118     }
0119     return 0;
0120 }