delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kioslave / bookmarks / kio_bookmarks.cpp
blobe37453cb951ba2d1b29c3adaa4d6b72971e554e7
1 /*
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"
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include <qregexp.h>
26 #include <kapplication.h>
27 #include <kcmdlineargs.h>
28 #include <kaboutdata.h>
30 #include <kshell.h>
31 #include <kstandarddirs.h>
32 #include <kcomponentdata.h>
33 #include <klocale.h>
34 #include <kconfig.h>
35 #include <kconfiggroup.h>
36 #include <kbookmark.h>
37 #include <kbookmarkmanager.h>
38 #include <kpixmapcache.h>
39 #include <kdebug.h>
40 #include <kfileplacesmodel.h>
41 #include <solid/device.h>
42 #include <solid/deviceinterface.h>
43 #include <ktoolinvocation.h>
45 using namespace KIO;
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));
56 indent = 0;
57 totalsize = 0;
58 columns = 4;
61 BookmarksProtocol::~BookmarksProtocol()
63 delete manager;
64 delete cache;
65 delete cfg;
68 void BookmarksProtocol::parseTree()
70 totalsize = 0;
72 cfg->reparseConfiguration();
73 columns = config.readEntry("Columns", 4);
74 if (columns < 1)
75 columns = 1;
77 manager->notifyCompleteChange("kio_bookmarks");
78 tree = manager->root();
80 if(tree.first().isNull())
81 return;
83 if(config.readEntry("FlattenTree", false))
84 flattenTree(tree);
86 KBookmarkGroup root;
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();
95 KBookmark next;
96 while(!bm.isNull())
98 next = tree.next(bm);
99 if (bm.isSeparator())
100 tree.deleteBookmark(bm);
101 else if (bm.isGroup())
102 totalsize += sizeOfGroup(bm.toGroup());
103 else
105 if(config.readEntry("ShowRoot", true))
106 root.addBookmark(bm);
108 tree.deleteBookmark(bm);
110 bm = next;
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");
127 } else {
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;
144 KBookmark next;
145 while (!bm.isNull())
147 if (bm.isGroup()) {
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);
158 prev = bm;
160 bm = next;
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))
170 if (bm.isGroup())
171 size += sizeOfGroup(bm.toGroup());
172 else
173 size += 1;
176 // CSS sets a min-height for toplevel folders
177 if (folder.parentGroup() == tree && size < 8 && real == false)
178 size = 8;
180 return size;
183 void BookmarksProtocol::get( const KUrl& url )
185 QString path = url.path();
186 QRegExp regexp("^/(background|icon)/([\\S]+)");
188 if (path.isEmpty() || path == "/") {
189 echoIndex();
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"));
198 } else {
199 echoHead();
200 echo("<p class=\"message\">" + i18n("Wrong request: %1",path) + "</p>");
202 finished();
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);
211 KApplication app;
213 BookmarksProtocol slave(argv[2], argv[3]);
214 slave.dispatchLoop();
216 return 0;