summaryrefslogtreecommitdiffstats
path: root/konqueror/preloader/preloader.cpp
blob: ecc6691da3a2bdc4e96bc194cec9e2a002ca9e17 (plain)
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
/* This file is part of the KDE project
   Copyright (C) 2002 Lubos Lunak <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "preloader.h"
#include "konq_settingsxt.h"

#include <tdeconfig.h>
#include <dcopref.h>
#include <tdeapplication.h>
#include <dcopclient.h>
#include <kdebug.h>

KonqyPreloader::KonqyPreloader( const TQCString& obj )
    : KDEDModule( obj )
    {
    reconfigure();
    connect( kapp->dcopClient(), TQ_SIGNAL( applicationRemoved( const TQCString& )),
        TQ_SLOT( appRemoved( const TQCString& )));
    connect( &check_always_preloaded_timer, TQ_SIGNAL( timeout()),
	TQ_SLOT( checkAlwaysPreloaded()));
    }

KonqyPreloader::~KonqyPreloader()
    {
    updateCount();
    }

bool KonqyPreloader::registerPreloadedKonqy( TQCString id, int screen )
    {
    if( instances.count() >= (uint)KonqSettings::maxPreloadCount() )
        return false;
    instances.append( KonqyData( id, screen ));
    return true;
    }

TQCString KonqyPreloader::getPreloadedKonqy( int screen )
    {
    if( instances.count() == 0 )
        return "";
    for( InstancesList::Iterator it = instances.begin();
         it != instances.end();
         ++it )
        {
        if( (*it).screen == screen )
            {
            TQCString ret = (*it).id;
            instances.remove( it );
            check_always_preloaded_timer.start( 5000, true );
            return ret;
            }
        }
    return "";
    }

void KonqyPreloader::unregisterPreloadedKonqy( TQCString id_P )
    {
    for( InstancesList::Iterator it = instances.begin();
         it != instances.end();
         ++it )
        if( (*it).id == id_P )
            {
            instances.remove( it );
            return;
            }
    }

void KonqyPreloader::appRemoved( const TQCString& id )
    {
    unregisterPreloadedKonqy( id );
    }
    
void KonqyPreloader::reconfigure()
    {
    KonqSettings::self()->readConfig();
    updateCount();
    // Ignore "PreloadOnStartup" here, it's used by the .desktop file
    // in the autostart folder, which will do 'konqueror --preload' in autostart
    // phase 2. This will also cause activation of this kded module.
    }

void KonqyPreloader::updateCount()
    {
    while( instances.count() > (uint)KonqSettings::maxPreloadCount() )
        {
        KonqyData konqy = instances.first();
        instances.pop_front();
        DCOPRef ref( konqy.id, "KonquerorIface" );
        ref.send( "terminatePreloaded" );
        }
    if( KonqSettings::alwaysHavePreloaded() &&
        KonqSettings::maxPreloadCount() > 0 &&
        instances.count() == 0 )
	{
	if( !check_always_preloaded_timer.isActive())
	    {
	    if( kapp->tdeinitExec( TQString::fromLatin1( "konqueror" ),
		TQStringList() << TQString::fromLatin1( "--preload" ), NULL, NULL, "0" ) == 0 )
		{
		kdDebug( 1202 ) << "Preloading Konqueror instance" << endl;
	        check_always_preloaded_timer.start( 5000, true );
		}
	    // else do nothing, the launching failed
	    }
	}
    }

// have 5s interval between attempts to preload a new konqy
// in order not to start many of them at the same time
void KonqyPreloader::checkAlwaysPreloaded()
    {
    // TODO here should be detection whether the system is too busy,
    // and delaying preloading another konqy in such case
    // but I have no idea how to do it
    updateCount();
    }
    
void KonqyPreloader::unloadAllPreloaded()
    {
    while( instances.count() > 0 )
        {
        KonqyData konqy = instances.first();
        instances.pop_front();
        DCOPRef ref( konqy.id, "KonquerorIface" );
        ref.send( "terminatePreloaded" );
        }
    // ignore 'always_have_preloaded' here
    }
    
extern "C"
KDE_EXPORT KDEDModule *create_konqy_preloader( const TQCString& obj )
    {
    return new KonqyPreloader( obj );
    }

#include "preloader.moc"