1 // -*- tab-width:4; indent-tabs-mode:t -*-
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
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>
31 #include <kstandarddirs.h>
34 #include <QtCore/QDir>
35 #include <QtXml/qdom.h>
36 #include <QtCore/QFile>
42 // The code for this function was taken from kdesktop/kcheckrunning.cpp
43 static bool kdeIsRunning()
46 Display
*dpy
= XOpenDisplay( NULL
);
51 Atom atom
= XInternAtom( dpy
, "_KDE_RUNNING", False
);
52 return XGetSelectionOwner( dpy
, atom
) != None
;
58 int main( int argc
, char**argv
)
60 const bool kdeRunning
= kdeIsRunning();
62 KAboutData
aboutData( "kbookmarkmerger", 0, ki18n( "KBookmarkMerger" ),
63 "1.0", ki18n( "Merges bookmarks installed by 3rd parties into the user's bookmarks" ),
64 KAboutData::License_BSD
,
65 ki18n( "Copyright © 2005 Frerich Raabe" ) );
66 aboutData
.addAuthor( ki18n("Frerich Raabe"), ki18n( "Original author" ),
69 KCmdLineArgs::init( argc
, argv
, &aboutData
);
71 KCmdLineOptions cmdLineOptions
;
72 cmdLineOptions
.add("+directory", ki18n( "Directory to scan for extra bookmarks" ));
73 KCmdLineArgs::addCmdLineOptions( cmdLineOptions
);
75 KApplication
app( false );
76 app
.disableSessionManagement();
78 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs();
79 if ( args
->count() != 1 ) {
80 kError() << "No directory to scan for bookmarks specified." << endl
;
84 KBookmarkManager
*konqBookmarks
= KBookmarkManager::userBookmarksManager();
85 QStringList mergedFiles
;
87 KBookmarkGroup root
= konqBookmarks
->root();
88 for ( KBookmark bm
= root
.first(); !bm
.isNull(); bm
= root
.next( bm
) ) {
93 QString mergedFrom
= bm
.metaDataItem( "merged_from" );
94 if ( !mergedFrom
.isNull() ) {
95 mergedFiles
<< mergedFrom
;
100 bool didMergeBookmark
= false;
102 QString extraBookmarksDirName
= args
->arg( 0 );
103 QDir
extraBookmarksDir( extraBookmarksDirName
, "*.xml" );
104 if ( !extraBookmarksDir
.isReadable() ) {
105 kError() << "Failed to read files in directory " << extraBookmarksDirName
<< endl
;
109 for ( unsigned int i
= 0; i
< extraBookmarksDir
.count(); ++i
) {
110 const QString fileName
= extraBookmarksDir
[ i
];
111 if ( mergedFiles
.contains( fileName
) ) {
115 const QString absPath
= extraBookmarksDir
.filePath( fileName
);
116 KBookmarkManager
*mgr
= KBookmarkManager::managerForFile( absPath
, QString() );
117 KBookmarkGroup root
= mgr
->root();
118 for ( KBookmark bm
= root
.first(); !bm
.isNull(); bm
= root
.next( bm
) ) {
119 if ( bm
.isGroup() ) {
122 bm
.setMetaDataItem( "merged_from", fileName
);
123 konqBookmarks
->root().addBookmark( bm
);
124 didMergeBookmark
= true;
128 if ( didMergeBookmark
) {
130 konqBookmarks
->emitChanged( konqBookmarks
->root() ); // calls save
131 //konqBookmarks->notifyChanged( "" );
133 if ( !konqBookmarks
->save() ) {
134 kError() << "Failed to write merged bookmarks." << endl
;