1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*
Copyright (C) 2008 Xavier Vello <xavier.vello@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kio_bookmarks.h"
#include <stdio.h>
#include <stdlib.h>
#include <qregexp.h>
#include <qtextdocument.h>
#include <qurlquery.h>
#include <kshell.h>
#include <KLocalizedString>
#include <kconfig.h>
#include <kconfiggroup.h>
#include <kbookmark.h>
#include <kbookmarkmanager.h>
#include <kimagecache.h>
#include <qdebug.h>
#include <kfileplacesmodel.h>
#include <solid/device.h>
#include <solid/deviceinterface.h>
#include <ktoolinvocation.h>
#include <QCoreApplication>
using namespace KIO;
BookmarksProtocol::BookmarksProtocol( const QByteArray &pool, const QByteArray &app )
: SlaveBase( "bookmarks", pool, app )
{
manager = KBookmarkManager::userBookmarksManager();
cfg = new KConfig( "kiobookmarksrc" );
config = cfg->group("General");
cache = new KImageCache("kio_bookmarks", config.readEntry("CacheSize", 5 * 1024) * 1024);
cache->setPixmapCaching(false);
indent = 0;
totalsize = 0;
columns = 4;
}
BookmarksProtocol::~BookmarksProtocol()
{
delete manager;
delete cache;
delete cfg;
}
void BookmarksProtocol::parseTree()
{
totalsize = 0;
cfg->reparseConfiguration();
columns = config.readEntry("Columns", 4);
if (columns < 1)
columns = 1;
manager->notifyCompleteChange("kio_bookmarks");
tree = manager->root();
if(tree.first().isNull())
return;
if(config.readEntry("FlattenTree", false))
flattenTree(tree);
KBookmarkGroup root;
if(config.readEntry("ShowRoot", true))
{
root = tree.createNewFolder(i18n("Root"));
tree.moveBookmark(root, KBookmark());
root.setIcon("konqueror");
}
KBookmark bm = tree.first();
KBookmark next;
while(!bm.isNull())
{
next = tree.next(bm);
if (bm.isSeparator())
tree.deleteBookmark(bm);
else if (bm.isGroup())
totalsize += sizeOfGroup(bm.toGroup());
else
{
if(config.readEntry("ShowRoot", true))
root.addBookmark(bm);
tree.deleteBookmark(bm);
}
bm = next;
}
if(config.readEntry("ShowRoot", true))
totalsize += sizeOfGroup(root);
if(config.readEntry("ShowPlaces", true))
totalsize += addPlaces();
}
int BookmarksProtocol::addPlaces()
{
KFilePlacesModel placesModel;
KBookmarkGroup folder = tree.createNewFolder(i18n("Places"));
QList<Solid::Device> batteryList = Solid::Device::listFromType(Solid::DeviceInterface::Battery, QString());
if (batteryList.isEmpty()) {
folder.setIcon("computer");
} else {
folder.setIcon("computer-laptop");
}
for (int row = 0; row < placesModel.rowCount(); ++row) {
QModelIndex index = placesModel.index(row, 0);
if (!placesModel.isHidden(index))
folder.addBookmark(placesModel.bookmarkForIndex(index));
}
return sizeOfGroup(folder);
}
void BookmarksProtocol::flattenTree( const KBookmarkGroup &folder )
{
KBookmark bm = folder.first();
KBookmark prev = folder;
KBookmark next;
while (!bm.isNull())
{
if (bm.isGroup()) {
flattenTree(bm.toGroup());
}
next = tree.next(bm);
if (bm.isGroup() && bm.parentGroup().hasParent()) {
//qDebug() << "moving " << bm.text() << " from " << bm.parentGroup().fullText() << " to " << prev.parentGroup().text() << endl;
bm.setFullText("| " + bm.parentGroup().fullText() + " > " + bm.fullText());
tree.moveBookmark(bm, prev);
prev = bm;
}
bm = next;
}
}
// Should really go to KBookmarkGroup
int BookmarksProtocol::sizeOfGroup( const KBookmarkGroup &folder, bool real )
{
int size = 1; // counting the title line
for (KBookmark bm = folder.first(); !bm.isNull(); bm = folder.next(bm))
{
if (bm.isGroup())
size += sizeOfGroup(bm.toGroup());
else
size += 1;
}
// CSS sets a min-height for toplevel folders
if (folder.parentGroup() == tree && size < 8 && real == false)
size = 8;
return size;
}
void BookmarksProtocol::get( const QUrl& url )
{
QString path = url.path();
QRegExp regexp("^/(background|icon)/([\\S]+)");
if (path.isEmpty() || path == "/") {
echoIndex();
} else if (path == "/config") {
KToolInvocation::startServiceByDesktopName("bookmarks", "");
echoHead("bookmarks:/");
} else if (path == "/editbookmarks") {
KToolInvocation::kdeinitExec("keditbookmarks");
echoHead("bookmarks:/");
} else if (regexp.indexIn(path) >= 0) {
echoImage(regexp.cap(1), regexp.cap(2), QUrlQuery(url).queryItemValue("size"));
} else {
echoHead();
echo("<p class=\"message\">" + i18n("Wrong request: %1", url.toDisplayString().toHtmlEscaped()) + "</p>");
}
finished();
}
extern "C" int Q_DECL_EXPORT kdemain(int argc, char **argv)
{
QCoreApplication app(argc, argv);
app.setApplicationName(QLatin1String("kio_ldap"));
if (argc != 4) {
qCritical() << "Usage: kio_bookmarks protocol domain-socket1 domain-socket2";
exit(-1);
}
BookmarksProtocol slave(argv[2], argv[3]);
slave.dispatchLoop();
return 0;
}
|