2 Copyright (C) 2008 Xavier Vello <xavier.vello@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "kio_bookmarks.h"
26 #include <kapplication.h>
27 #include <kcmdlineargs.h>
28 #include <kaboutdata.h>
31 #include <kstandarddirs.h>
32 #include <kcomponentdata.h>
35 #include <kconfiggroup.h>
36 #include <kbookmark.h>
37 #include <kbookmarkmanager.h>
38 #include <kpixmapcache.h>
40 #include <kfileplacesmodel.h>
41 #include <solid/device.h>
42 #include <solid/deviceinterface.h>
43 #include <ktoolinvocation.h>
47 BookmarksProtocol::BookmarksProtocol( const QByteArray
&pool
, const QByteArray
&app
)
48 : SlaveBase( "bookmarks", pool
, app
)
50 manager
= KBookmarkManager::userBookmarksManager();
51 cfg
= new KConfig( "kiobookmarksrc" );
52 config
= cfg
->group("General");
53 cache
= new KPixmapCache("kio_bookmarks");
54 cache
->setCacheLimit(config
.readEntry("CacheSize", 5*1024));
61 BookmarksProtocol::~BookmarksProtocol()
68 void BookmarksProtocol::parseTree()
72 cfg
->reparseConfiguration();
73 columns
= config
.readEntry("Columns", 4);
77 manager
->notifyCompleteChange("kio_bookmarks");
78 tree
= manager
->root();
80 if(tree
.first().isNull())
83 if(config
.readEntry("FlattenTree", false))
87 if(config
.readEntry("ShowRoot", true))
89 root
= tree
.createNewFolder(i18n("Root"));
90 tree
.moveBookmark(root
, KBookmark());
91 root
.setIcon("konqueror");
94 KBookmark bm
= tree
.first();
100 tree
.deleteBookmark(bm
);
101 else if (bm
.isGroup())
102 totalsize
+= sizeOfGroup(bm
.toGroup());
105 if(config
.readEntry("ShowRoot", true))
106 root
.addBookmark(bm
);
108 tree
.deleteBookmark(bm
);
112 if(config
.readEntry("ShowRoot", true))
113 totalsize
+= sizeOfGroup(root
);
115 if(config
.readEntry("ShowPlaces", true))
116 totalsize
+= addPlaces();
119 int BookmarksProtocol::addPlaces()
121 KFilePlacesModel placesModel
;
122 KBookmarkGroup folder
= tree
.createNewFolder(i18n("Places"));
123 QList
<Solid::Device
> batteryList
= Solid::Device::listFromType(Solid::DeviceInterface::Battery
, QString());
125 if (batteryList
.isEmpty()) {
126 folder
.setIcon("computer");
128 folder
.setIcon("computer-laptop");
131 for (int row
= 0; row
< placesModel
.rowCount(); ++row
) {
132 QModelIndex index
= placesModel
.index(row
, 0);
134 if (!placesModel
.isHidden(index
))
135 folder
.addBookmark(placesModel
.bookmarkForIndex(index
));
137 return sizeOfGroup(folder
);
140 void BookmarksProtocol::flattenTree( const KBookmarkGroup
&folder
)
142 KBookmark bm
= folder
.first();
143 KBookmark prev
= folder
;
148 flattenTree(bm
.toGroup());
151 next
= tree
.next(bm
);
153 if (bm
.isGroup() && bm
.parentGroup().hasParent()) {
154 kDebug() << "moving " << bm
.text() << " from " << bm
.parentGroup().fullText() << " to " << prev
.parentGroup().text() << endl
;
156 bm
.setFullText("| " + bm
.parentGroup().fullText() + " > " + bm
.fullText());
157 tree
.moveBookmark(bm
, prev
);
164 // Should really go to KBookmarkGroup
165 int BookmarksProtocol::sizeOfGroup( const KBookmarkGroup
&folder
, bool real
)
167 int size
= 1; // counting the title line
168 for (KBookmark bm
= folder
.first(); !bm
.isNull(); bm
= folder
.next(bm
))
171 size
+= sizeOfGroup(bm
.toGroup());
176 // CSS sets a min-height for toplevel folders
177 if (folder
.parentGroup() == tree
&& size
< 8 && real
== false)
183 void BookmarksProtocol::get( const KUrl
& url
)
185 QString path
= url
.path();
186 QRegExp
regexp("^/(background|icon)/([\\S]+)");
188 if (path
.isEmpty() || path
== "/") {
190 } else if (path
== "/config") {
191 KToolInvocation::startServiceByDesktopName("bookmarks", "");
192 echoHead("bookmarks:/");
193 } else if (path
== "/editbookmarks") {
194 KToolInvocation::kdeinitExec("keditbookmarks");
195 echoHead("bookmarks:/");
196 } else if (regexp
.indexIn(path
) >= 0) {
197 echoImage(regexp
.cap(1), regexp
.cap(2), url
.queryItem("size"));
200 echo("<p class=\"message\">" + i18n("Wrong request: %1",path
) + "</p>");
205 extern "C" int KDE_EXPORT
kdemain(int argc
, char **argv
)
207 KAboutData
about("kio_bookmarks", 0, ki18n("My bookmarks"), "0.2.2");
208 about
.addLicense(KAboutData::License_GPL_V2
);
209 about
.addAuthor(ki18n("Xavier Vello"), ki18n("Initial developer"), "xavier.vello@gmail.com", QByteArray());
210 KCmdLineArgs::init(&about
);
213 BookmarksProtocol
slave(argv
[2], argv
[3]);
214 slave
.dispatchLoop();