delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / konqueror / preloader / preloader.cc
blob6dd92130ba9bc8f6067b754918c6064ba6c879cc
1 /* This file is part of the KDE project
2 Copyright (C) 2002 Lubos Lunak <l.lunak@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "preloader.h"
21 #include "konqsettingsxt.h"
22 #include "preloaderadaptor.h"
24 #include <kdebug.h>
25 #include <ktoolinvocation.h>
27 #include <kpluginfactory.h>
28 #include <kpluginloader.h>
30 K_PLUGIN_FACTORY(KonqyPreloaderFactory,
31 registerPlugin<KonqyPreloader>();
33 K_EXPORT_PLUGIN(KonqyPreloaderFactory("konqypreloader"))
35 KonqyPreloader::KonqyPreloader(QObject* parent, const QList<QVariant>&)
36 : KDEDModule(parent)
38 reconfigure();
40 (void)new PreloaderAdaptor(this);
42 connect( QDBusConnection::sessionBus().interface(),
43 SIGNAL( serviceOwnerChanged( const QString&, const QString&, const QString& )),
44 SLOT ( appChanged( const QString&, const QString&, const QString& )));
45 check_always_preloaded_timer.setSingleShot( true );
46 connect( &check_always_preloaded_timer, SIGNAL( timeout()),
47 SLOT( checkAlwaysPreloaded()));
50 KonqyPreloader::~KonqyPreloader()
52 updateCount();
55 bool KonqyPreloader::registerPreloadedKonqy( const QString &id, int screen )
57 if( instances.count() >= KonqSettings::maxPreloadCount() )
58 return false;
59 instances.append( KonqyData( id, screen ));
60 return true;
63 QString KonqyPreloader::getPreloadedKonqy( int screen )
65 if( instances.count() == 0 )
66 return "";
67 for( InstancesList::Iterator it = instances.begin();
68 it != instances.end();
69 ++it )
71 if( (*it).screen == screen )
73 QString ret = (*it).id;
74 instances.erase( it );
75 check_always_preloaded_timer.start( 5000 );
76 return ret;
79 return "";
82 void KonqyPreloader::unregisterPreloadedKonqy( const QString &id_P )
84 for( InstancesList::Iterator it = instances.begin();
85 it != instances.end();
86 ++it )
87 if( (*it).id == id_P )
89 instances.erase( it );
90 return;
94 void KonqyPreloader::appChanged( const QString & /*id*/, const QString &oldOwner, const QString &newOwner )
96 if ( oldOwner.isEmpty() || !newOwner.isEmpty() )
97 return;
99 unregisterPreloadedKonqy( oldOwner );
102 void KonqyPreloader::reconfigure()
104 KonqSettings::self()->readConfig();
105 updateCount();
106 // Ignore "PreloadOnStartup" here, it's used by the .desktop file
107 // in the autostart folder, which will do 'konqueror --preload' in autostart
108 // phase 2. This will also cause activation of this kded module.
111 void KonqyPreloader::updateCount()
113 while( instances.count() > KonqSettings::maxPreloadCount() )
115 KonqyData konqy = instances.first();
116 instances.pop_front();
117 QDBusInterface ref( konqy.id, "/", "org.kde.Konqueror.Main" );
118 ref.call( "terminatePreloaded" );
120 if( KonqSettings::alwaysHavePreloaded() &&
121 KonqSettings::maxPreloadCount() > 0 &&
122 instances.count() == 0 )
124 if( !check_always_preloaded_timer.isActive())
126 if( KToolInvocation::kdeinitExec( QLatin1String( "konqueror" ),
127 QStringList() << QLatin1String( "--preload" ), NULL, NULL, "0" ) == 0 )
129 kDebug( 1202 ) << "Preloading Konqueror instance";
130 check_always_preloaded_timer.start( 5000 );
132 // else do nothing, the launching failed
137 // have 5s interval between attempts to preload a new konqy
138 // in order not to start many of them at the same time
139 void KonqyPreloader::checkAlwaysPreloaded()
141 // TODO here should be detection whether the system is too busy,
142 // and delaying preloading another konqy in such case
143 // but I have no idea how to do it
144 updateCount();
147 void KonqyPreloader::unloadAllPreloaded()
149 while( instances.count() > 0 )
151 KonqyData konqy = instances.first();
152 instances.pop_front();
153 QDBusInterface ref( konqy.id, "/", "org.kde.Konqueror.Main" );
154 ref.call( "terminatePreloaded" );
156 // ignore 'always_have_preloaded' here
159 #include "preloader.moc"