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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
/* This file is part of the KDE project
Copyright 2007 David Faure <faure@kde.org>
Copyright 2007 Eduardo Robles Elvira <edulix@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; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "konqclosedwindowsmanager.h"
#include "konqsettingsxt.h"
#include "konqmisc.h"
#include "konqcloseditem.h"
#include "konqclosedwindowsmanageradaptor.h"
#include "konqclosedwindowsmanager_interface.h"
#include <kio/fileundomanager.h>
#include <QDirIterator>
#include <QMetaType>
#include <QtDBus/QtDBus>
#include <kglobal.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kconfig.h>
#include <unistd.h> // getpid
Q_DECLARE_METATYPE(QList<QVariant>)
class KonqClosedWindowsManagerPrivate
{
public:
KonqClosedWindowsManager instance;
int m_maxNumClosedItems;
};
K_GLOBAL_STATIC(KonqClosedWindowsManagerPrivate, myKonqClosedWindowsManagerPrivate)
KonqClosedWindowsManager::KonqClosedWindowsManager()
{
new KonqClosedWindowsManagerAdaptor ( this );
const QString dbusPath = "/KonqUndoManager";
const QString dbusInterface = "org.kde.Konqueror.UndoManager";
QDBusConnection dbus = QDBusConnection::sessionBus();
dbus.registerObject( dbusPath, this );
dbus.connect(QString(), dbusPath, dbusInterface, "notifyClosedWindowItem", this, SLOT(slotNotifyClosedWindowItem(QString,int,QString,QString,QDBusMessage)));
dbus.connect(QString(), dbusPath, dbusInterface, "notifyRemove", this, SLOT(slotNotifyRemove(QString,QString,QDBusMessage)));
QString filename = "closeditems/" + KonqMisc::encodeFilename(dbus.baseService());
QString file = KStandardDirs::locateLocal("tmp", filename);
QFile::remove(file);
KConfigGroup configGroup(KGlobal::config(), "Undo");
m_numUndoClosedItems = configGroup.readEntry("Number of Closed Windows", 0);
m_konqClosedItemsConfig = 0L;
m_blockClosedItems = false;
m_konqClosedItemsStore = new KConfig(filename, KConfig::SimpleConfig, "tmp");
}
KonqClosedWindowsManager::~KonqClosedWindowsManager()
{
// Do some file cleaning
removeClosedItemsConfigFiles();
qDeleteAll(m_closedWindowItemList); // must be done before deleting the kconfigs
delete m_konqClosedItemsConfig;
delete m_konqClosedItemsStore;
}
KConfig* KonqClosedWindowsManager::memoryStore()
{
return m_konqClosedItemsStore;
}
KonqClosedWindowsManager *KonqClosedWindowsManager::self()
{
return &myKonqClosedWindowsManagerPrivate->instance;
}
void KonqClosedWindowsManager::addClosedWindowItem(KonqUndoManager
*real_sender, KonqClosedWindowItem *closedWindowItem, bool propagate)
{
readConfig();
// If we are off the limit, remove the last closed window item
if(m_closedWindowItemList.size() >=
KonqSettings::maxNumClosedItems())
{
KonqClosedWindowItem* last = m_closedWindowItemList.last();
emit removeWindowInOtherInstances(0L, last);
emitNotifyRemove(last);
m_closedWindowItemList.removeLast();
delete last;
}
if(!m_blockClosedItems)
{
m_numUndoClosedItems++;
emit addWindowInOtherInstances(real_sender, closedWindowItem);
}
// The prepend goes after emit addWindowInOtherInstances() because otherwise
// the first time addWindowInOtherInstances() is emitted, KonqUndoManager
// will catch it and it will call to its private populate() function which
// will add to the undo list closedWindowItem, and then it will add it again
// but we want it to be added only once.
m_closedWindowItemList.prepend(closedWindowItem);
if(propagate)
{
// if it needs to be propagated means that it's a local window and thus
// we need to call to saveConfig() to keep updated the kconfig file, so
// that new konqueror instances can read it correctly updated.
saveConfig();
// Once saved, tell to other konqi processes
emitNotifyClosedWindowItem(closedWindowItem);
}
}
void KonqClosedWindowsManager::removeClosedWindowItem(KonqUndoManager
*real_sender, const KonqClosedWindowItem *closedWindowItem, bool propagate)
{
readConfig();
QList<KonqClosedWindowItem *>::iterator it = qFind(m_closedWindowItemList.begin(),
m_closedWindowItemList.end(), closedWindowItem);
// If the item was found, remove it from the list
if(it != m_closedWindowItemList.end()) {
m_closedWindowItemList.erase(it);
m_numUndoClosedItems--;
}
emit removeWindowInOtherInstances(real_sender, closedWindowItem);
if(propagate)
emitNotifyRemove(closedWindowItem);
}
const QList<KonqClosedWindowItem *>& KonqClosedWindowsManager::closedWindowItemList()
{
readConfig();
return m_closedWindowItemList;
}
void KonqClosedWindowsManager::readSettings()
{
readConfig();
}
static QString dbusService()
{
return QDBusConnection::sessionBus().baseService();
}
/**
* Returns whether the DBUS call we are handling was a call from us self
*/
bool isSenderOfSignal( const QDBusMessage& msg )
{
return dbusService() == msg.service();
}
bool isSenderOfSignal( const QString& service )
{
return dbusService() == service;
}
void KonqClosedWindowsManager::emitNotifyClosedWindowItem(
const KonqClosedWindowItem *closedWindowItem)
{
QString filename = "closeditems/" + KonqMisc::encodeFilename(QDBusConnection::sessionBus().baseService());
QString file = KStandardDirs::locateLocal("tmp", filename);
emit notifyClosedWindowItem( closedWindowItem->title(),
closedWindowItem->numTabs(),
m_konqClosedItemsStore->name(),
closedWindowItem->configGroup().name() );
}
void KonqClosedWindowsManager::emitNotifyRemove(
const KonqClosedWindowItem *closedWindowItem)
{
const KonqClosedRemoteWindowItem* closedRemoteWindowItem =
dynamic_cast<const KonqClosedRemoteWindowItem *>(closedWindowItem);
// Here we do this because there's no need to call to configGroup() if it's
// a remote window item, and it would be error prone to be so, because
// it could give us a null pointer and konqueror would crash
if(closedRemoteWindowItem)
emit notifyRemove( closedRemoteWindowItem->remoteConfigFileName(),
closedRemoteWindowItem->remoteGroupName() );
else
emit notifyRemove( closedWindowItem->configGroup().config()->name(),
closedWindowItem->configGroup().name() );
}
void KonqClosedWindowsManager::slotNotifyClosedWindowItem(
const QString& title, const int& numTabs, const QString& configFileName,
const QString& configGroup, const QString& service )
{
if ( isSenderOfSignal( service ) )
return;
// Create a new ClosedWindowItem and add it to the list
KonqClosedWindowItem* closedWindowItem = new KonqClosedRemoteWindowItem(
title, configGroup, configFileName,
KIO::FileUndoManager::self()->newCommandSerialNumber(), numTabs,
service);
// Add it to all the windows but don't propagate over dbus,
// as it already comes from dbus)
addClosedWindowItem(0L, closedWindowItem, false);
}
void KonqClosedWindowsManager::slotNotifyClosedWindowItem(
const QString& title, const int& numTabs, const QString& configFileName,
const QString& configGroup, const QDBusMessage& msg )
{
slotNotifyClosedWindowItem(title, numTabs, configFileName, configGroup,
msg.service() );
}
void KonqClosedWindowsManager::slotNotifyRemove(
const QString& configFileName, const QString& configGroup,
const QDBusMessage& msg )
{
if ( isSenderOfSignal( msg ) )
return;
// Find the window item. It can be either remote or local
KonqClosedWindowItem* closedWindowItem =
findClosedRemoteWindowItem(configFileName, configGroup);
if(!closedWindowItem)
{
closedWindowItem = findClosedLocalWindowItem(configFileName, configGroup);
if(!closedWindowItem)
return;
}
// Remove it in all the windows but don't propagate over dbus,
// as it already comes from dbus)
removeClosedWindowItem(0L, closedWindowItem, false);
}
KonqClosedRemoteWindowItem* KonqClosedWindowsManager::findClosedRemoteWindowItem(
const QString& configFileName,
const QString& configGroup)
{
readConfig();
KonqClosedRemoteWindowItem* closedRemoteWindowItem = 0L;
for (QList<KonqClosedWindowItem *>::const_iterator it = m_closedWindowItemList.constBegin();
it != m_closedWindowItemList.constEnd(); ++it)
{
closedRemoteWindowItem = dynamic_cast<KonqClosedRemoteWindowItem *>(*it);
if(closedRemoteWindowItem &&
closedRemoteWindowItem->equalsTo(configFileName, configGroup))
return closedRemoteWindowItem;
}
return closedRemoteWindowItem;
}
KonqClosedWindowItem* KonqClosedWindowsManager::findClosedLocalWindowItem(
const QString& configFileName,
const QString& configGroup)
{
readConfig();
KonqClosedWindowItem* closedWindowItem = 0L;
for (QList<KonqClosedWindowItem *>::const_iterator it = m_closedWindowItemList.constBegin();
it != m_closedWindowItemList.constEnd(); ++it)
{
closedWindowItem = *it;
KonqClosedRemoteWindowItem* closedRemoteWindowItem =
dynamic_cast<KonqClosedRemoteWindowItem *>(closedWindowItem);
if(!closedRemoteWindowItem && closedWindowItem &&
closedWindowItem->configGroup().config()->name() == configFileName &&
closedWindowItem->configGroup().name() == configGroup)
return closedWindowItem;
}
return closedWindowItem;
}
/**
* @returns the number of konqueror processes by counting the number of
* org.kde.konqueror services in dbus.
*
* If dbus fails it returns -1.
*/
static int numberOfKonquerorProcesses()
{
QDBusConnection dbus = QDBusConnection::sessionBus();
QDBusReply<QStringList> reply = dbus.interface()->registeredServiceNames();
if ( !reply.isValid() )
return -1;
const QStringList allServices = reply;
int count = 0; // count the number of running konqueror processes. Should be at least one, us.
for ( QStringList::const_iterator it = allServices.begin(), end = allServices.end() ; it != end ; ++it ) {
const QString service = *it;
if ( service.startsWith( "org.kde.konqueror" ) ) {
count++;
}
}
return count;
}
void KonqClosedWindowsManager::removeClosedItemsConfigFiles()
{
// We'll only remove closed items config files if we are the only process
// left or if dbus fails (just in case there is any other konqi process
// but we couldn't see it).
int count = numberOfKonquerorProcesses();
if(count > 1 || count == -1)
return;
// We are the only instance of konqueror left and thus we can safely remove
// all those temporary files.
QString dir = KStandardDirs::locateLocal("tmp", "closeditems/");
QDBusConnectionInterface *idbus = QDBusConnection::sessionBus().interface();
QDirIterator it(dir, QDir::Writable|QDir::Files);
while (it.hasNext())
{
// Only remove the files for those konqueror instances not running anymore
QString filename = it.next();
if(!idbus->isServiceRegistered(KonqMisc::decodeFilename(it.fileName())))
QFile::remove(filename);
}
}
void KonqClosedWindowsManager::saveConfig()
{
readConfig();
// Create / overwrite the saved closed windows list
QString filename = "closeditems_saved";
QString file = KStandardDirs::locateLocal("appdata", filename);
QFile::remove(file);
KConfig *config = new KConfig(filename, KConfig::SimpleConfig, "appdata");
// Populate the config file
KonqClosedWindowItem* closedWindowItem = 0L;
uint counter = m_closedWindowItemList.size()-1;
for (QList<KonqClosedWindowItem *>::const_iterator it = m_closedWindowItemList.constBegin();
it != m_closedWindowItemList.constEnd(); ++it, --counter)
{
closedWindowItem = *it;
KConfigGroup configGroup(config, "Closed_Window" + QString::number(counter));
configGroup.writeEntry("title", closedWindowItem->title());
configGroup.writeEntry("numTabs", closedWindowItem->numTabs());
closedWindowItem->configGroup().copyTo(&configGroup);
}
KConfigGroup configGroup(KGlobal::config(), "Undo");
configGroup.writeEntry("Number of Closed Windows", m_closedWindowItemList.size());
configGroup.sync();
// Finally the most important thing, which is to save the store config
// so that other konqi processes can reopen windows closed in this process.
m_konqClosedItemsStore->sync();
delete config;
}
void KonqClosedWindowsManager ::readConfig()
{
if(m_konqClosedItemsConfig)
return;
QString filename = "closeditems_saved";
QString file = KStandardDirs::locateLocal("appdata", filename);
m_konqClosedItemsConfig = new KConfig(file, KConfig::SimpleConfig);
// If the config file doesn't exists, there's nothing to read
if(!QFile::exists(file))
return;
m_blockClosedItems = true;
for(int i = 0; i < m_numUndoClosedItems; i++)
{
// For each item, create a new ClosedWindowItem
KConfigGroup configGroup(m_konqClosedItemsConfig, "Closed_Window" +
QString::number(i));
// The number of closed items was not correctly set, fix it and save the
// correct number.
if(!configGroup.exists())
{
m_numUndoClosedItems = i;
KConfigGroup configGroup(KGlobal::config(), "Undo");
configGroup.writeEntry("Number of Closed Windows",
m_closedWindowItemList.size());
configGroup.sync();
break;
}
QString title = configGroup.readEntry("title", i18n("no name"));
int numTabs = configGroup.readEntry("numTabs", 0);
KonqClosedWindowItem* closedWindowItem = new KonqClosedWindowItem(
title, i, numTabs);
configGroup.copyTo(&closedWindowItem->configGroup());
configGroup.writeEntry("foo", 0);
// Add the item only to this window
addClosedWindowItem(0L, closedWindowItem, false);
}
m_blockClosedItems = false;
}
bool KonqClosedWindowsManager::undoAvailable() const
{
return m_numUndoClosedItems > 0;
}
|