Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / keditbookmarks / kbookmarkmerger.cpp
blobb5b8d649b4b5385b4e0c3b97bebeca0544ab9ee7
1 // -*- tab-width:4; indent-tabs-mode:t -*-
2 /**
3 * kbookmarkmerger.cpp - Copyright (C) 2005 Frerich Raabe <raabe@kde.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <kaboutdata.h>
27 #include <kapplication.h>
28 #include <kbookmarkmanager.h>
29 #include <kcmdlineargs.h>
30 #include <kdebug.h>
31 #include <kstandarddirs.h>
32 #include <klocale.h>
34 #include <QtCore/QDir>
35 #include <QtXml/qdom.h>
37 int main( int argc, char**argv )
39 KAboutData aboutData( "kbookmarkmerger", "keditbookmarks", ki18n( "KBookmarkMerger" ),
40 "1.0", ki18n( "Merges bookmarks installed by 3rd parties into the user's bookmarks" ),
41 KAboutData::License_BSD,
42 ki18n( "Copyright © 2005 Frerich Raabe" ) );
43 aboutData.addAuthor( ki18n("Frerich Raabe"), ki18n( "Original author" ),
44 "raabe@kde.org" );
46 KCmdLineArgs::init( argc, argv, &aboutData );
48 KCmdLineOptions cmdLineOptions;
49 cmdLineOptions.add("+directory", ki18n( "Directory to scan for extra bookmarks" ));
50 KCmdLineArgs::addCmdLineOptions( cmdLineOptions );
52 KApplication app( false );
53 app.disableSessionManagement();
55 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
56 if ( args->count() != 1 ) {
57 kError() << "No directory to scan for bookmarks specified." << endl;
58 return 1;
61 KBookmarkManager *konqBookmarks = KBookmarkManager::userBookmarksManager();
62 QStringList mergedFiles;
64 KBookmarkGroup root = konqBookmarks->root();
65 for ( KBookmark bm = root.first(); !bm.isNull(); bm = root.next( bm ) ) {
66 if ( bm.isGroup() ) {
67 continue;
70 QString mergedFrom = bm.metaDataItem( "merged_from" );
71 if ( !mergedFrom.isNull() ) {
72 mergedFiles << mergedFrom;
77 bool didMergeBookmark = false;
79 QString extraBookmarksDirName = args->arg( 0 );
80 QDir extraBookmarksDir( extraBookmarksDirName, "*.xml" );
81 if ( !extraBookmarksDir.isReadable() ) {
82 kError() << "Failed to read files in directory " << extraBookmarksDirName << endl;
83 return 1;
86 for ( unsigned int i = 0; i < extraBookmarksDir.count(); ++i ) {
87 const QString fileName = extraBookmarksDir[ i ];
88 if ( mergedFiles.contains( fileName ) ) {
89 continue;
92 const QString absPath = extraBookmarksDir.filePath( fileName );
93 KBookmarkManager *mgr = KBookmarkManager::managerForFile( absPath, QString() );
94 KBookmarkGroup root = mgr->root();
95 for ( KBookmark bm = root.first(); !bm.isNull(); bm = root.next( bm ) ) {
96 if ( bm.isGroup() ) {
97 continue;
99 bm.setMetaDataItem( "merged_from", fileName );
100 konqBookmarks->root().addBookmark( bm );
101 didMergeBookmark = true;
105 if ( didMergeBookmark ) {
106 konqBookmarks->emitChanged( konqBookmarks->root() ); // calls save
107 // see TODO in emitChanged... if it returns false, it would be nice to return 1
108 // here.
110 return 0;