summaryrefslogtreecommitdiffstats
path: root/tdescreensaver/tdepartsaver/tdepartsaver.cpp
blob: a2c0cc407fe3c71a605baea953caa081e8985f71 (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
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
/*
 * Copyright (C) 2001 Stefan Schimanski <1Stein@gmx.de>
 *
 * This program 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 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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 <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>

#include <tqwidget.h>
#include <tqdialog.h>
#include <tqtimer.h>
#include <tqstring.h>
#include <tqvaluelist.h>
#include <tqpushbutton.h>
#include <tqradiobutton.h>
#include <tqspinbox.h>
#include <tqcheckbox.h>
#include <tqlabel.h>

#include <klocale.h>
#include <kapplication.h>
#include <kdebug.h>
#include <klibloader.h>
#include <kconfig.h>
#include <kfiledialog.h>
#include <kurl.h>
#include <tdeparts/part.h>
#include <ktrader.h>
#include <kio/jobclasses.h>
#include <kio/job.h>
#include <kmimetype.h>

#include <tdescreensaver.h>

#include "configwidget.h"
#include "tdepartsaver.h"
#include <kiconloader.h>


TQPtrList<KPartSaver> g_savers;
bool g_inited = false;


extern "C"
{
    KDE_EXPORT const char *kss_applicationName = "tdepartsaver";
    KDE_EXPORT const char *kss_description = I18N_NOOP( "KPart Screen Saver" );
    KDE_EXPORT const char *kss_version = "1.0";

    KDE_EXPORT KScreenSaver *kss_create( WId d )
    {
        TDEGlobal::locale()->insertCatalogue("tdepartsaver");
        return new KPartSaver( d );
    }

    KDE_EXPORT TQDialog *kss_setup()
    {
        kdDebug() << "kss_setup" << endl;
        TDEGlobal::locale()->insertCatalogue("tdepartsaver");
        return new SaverConfig;
    }
}


void exitHandler( int )
{
    kdDebug() << "exitHandler" << endl;
    g_savers.clear();
    KLibLoader::self()->cleanUp();
    exit(0);
}


KPartSaver::KPartSaver( WId id )
    : KScreenSaver( id ), m_timer(), m_part(0), m_current(-1), m_back(0)
{
    // install signal handlers to make sure that nspluginviewer is shutdown correctly
    // move this into the nspluginviewer kpart code
    if( !g_inited ) {
        g_inited = true;
        g_savers.setAutoDelete( true );

        srand( time(0) );

        // install signal handler
        signal( SIGINT, exitHandler );    // Ctrl-C will cause a clean exit...
        signal( SIGTERM, exitHandler );   // "kill"...
        signal( SIGHUP, exitHandler );    // "kill -HUP" (hangup)...
        signal( SIGKILL, exitHandler );    // "kill -KILL"
        //atexit( ( void (*)(void) ) exitHandler );
    }

    g_savers.append( this );

    closeURL();

    // load config
    TDEConfig *cfg = kapp->config();
    cfg->setGroup( "Misc" );

    m_single = cfg->readBoolEntry( "Single", true );
    m_delay = cfg->readNumEntry( "Delay", 60 );
    m_random = cfg->readBoolEntry( "Random", false );
    m_files = cfg->readListEntry( "Files" );

    if( m_files.count()==0 ) {

        // create background widget
        m_back = new TQLabel( i18n("The screen saver is not configured yet."), this );

        m_back->setAlignment( AlignCenter );
        embed( m_back );
        m_back->show();

    } else {

        // queue files
        for( unsigned int n=0; n<m_files.count(); n++ )
            queue( KURL::fromPathOrURL( m_files[n] ) );

        // play files
        if( m_single )
            next( m_random );
        else {
            next( m_random );
            m_timer = new TQTimer( this );
            m_timer->start( m_delay*1000, true );
            connect( m_timer, TQT_SIGNAL(timeout()), TQT_SLOT(timeout()) );
        }
    }
}


KPartSaver::~KPartSaver()
{
    g_savers.take( g_savers.findRef( this ));
    closeURL();
}


void KPartSaver::closeURL()
{
    if( m_part ) {
        m_part->closeURL();
        delete m_part;
        m_part = 0;
    }
}


bool KPartSaver::openURL( KURL url )
{
    closeURL();

    // find mime type
    TQString mime = KMimeType::findByURL( url )->name();

    // find fitting tdeparts
    KTrader::OfferList offers;
    offers = KTrader::self()->query( mime, "'KParts/ReadOnlyPart' in ServiceTypes" );
    if( offers.count()==0 ) {
        kdDebug() << "Can't find proper kpart for " << mime << endl;
        return false;
    }

    // load kpart library
    TQString lib = offers.first()->library();
    KLibFactory *factory = KLibLoader::self()->factory( lib.latin1() );
    if( !factory ) {
        kdDebug() << "Library " << lib << " not found." << endl;
        return false;
    }

    // create kpart
    m_part = (KParts::ReadOnlyPart *)factory->create( TQT_TQOBJECT(this), "kpart", "KParts::ReadOnlyPart" );
    if( !m_part ) {
        kdDebug() << "Part for " << url.url() << " can't be constructed" << endl;
        return false;
    } else
        embed( m_part->widget() );

    // show kpart
    delete m_back;
    m_back = 0;

    show();
    m_part->widget()->show();

    // load url
    if( !m_part->openURL( url ) ) {
        kdDebug() << "Can't load " << url.url() << endl;
        closeURL();
        return false;
    }



    return true;
}


void KPartSaver::queue( KURL url )
{
    Medium medium;
    medium.url = url;
    medium.failed = false;
    m_media.append( medium );
}


void KPartSaver::timeout()
{
    next( m_random );
    m_timer->start( m_delay*1000, true );
}


void KPartSaver::next( bool random )
{
    // try to find working media
    while( m_media.count()>0 ) {

        if( random )
            m_current = rand() % m_media.count();
        else
            m_current++;

        if( m_current>=(int)m_media.count() )
            m_current = 0;

        kdDebug() << "Trying medium " << m_media[m_current].url.url() << endl;

        // either start immediately or start mimejob first
        if( !openURL( m_media[m_current].url ) ) {
            m_media.remove( m_media.at(m_current) );
            m_current--;
        } else
            return;

    }

    // create background widget
    m_back = new TQLabel( i18n("All of your files are unsupported"), this );

    m_back->setAlignment( AlignCenter );
    embed( m_back );
    m_back->show();

    // nothing found, set to invalid
    m_current = -1;
}


/*******************************************************************************/


SaverConfig::SaverConfig( TQWidget* parent, const char* name )
    : ConfigWidget( parent, name, true )
{
    connect( m_ok, TQT_SIGNAL(clicked()), TQT_SLOT(apply()) );
    connect( m_ok, TQT_SIGNAL(clicked()), TQT_SLOT(accept()) );
    connect( m_cancel, TQT_SIGNAL(clicked()), TQT_SLOT(reject()) );

    connect( m_multiple, TQT_SIGNAL(toggled(bool)), m_delayLabel, TQT_SLOT(setEnabled(bool)) );
    connect( m_multiple, TQT_SIGNAL(toggled(bool)), m_delay, TQT_SLOT(setEnabled(bool)) );
    connect( m_multiple, TQT_SIGNAL(toggled(bool)), m_secondsLabel, TQT_SLOT(setEnabled(bool)) );
    connect( m_multiple, TQT_SIGNAL(toggled(bool)), m_random, TQT_SLOT(setEnabled(bool)) );

    connect( m_files, TQT_SIGNAL(selectionChanged()), TQT_SLOT(select()) );
    connect( m_add, TQT_SIGNAL(clicked()), TQT_SLOT(add()) );
    connect( m_remove, TQT_SIGNAL(clicked()), TQT_SLOT(remove()) );
    connect( m_up, TQT_SIGNAL(clicked()), TQT_SLOT(up()) );
    connect( m_down, TQT_SIGNAL(clicked()), TQT_SLOT(down()) );

    m_up->setIconSet( SmallIconSet("up") );
    m_down->setIconSet( SmallIconSet("down") );

    // load config
    TDEConfig *cfg = kapp->config();
    cfg->setGroup( "Misc" );

    bool single = cfg->readBoolEntry( "Single", true );
    m_single->setChecked( single );
    m_multiple->setChecked( !single );
    m_delay->setMinValue( 1 );
    m_delay->setMaxValue( 10000 );
    m_delay->setValue( cfg->readNumEntry( "Delay", 60 ) );
    m_random->setChecked( cfg->readBoolEntry( "Random", false ) );
    m_files->insertStringList( cfg->readListEntry( "Files" ) );

    // update buttons
    select();
}


SaverConfig::~SaverConfig()
{
}


void SaverConfig::apply()
{
    kdDebug() << "apply" << endl;

    TDEConfig *cfg = kapp->config();
    cfg->setGroup( "Misc" );

    cfg->writeEntry( "Single", m_single->isChecked() );
    cfg->writeEntry( "Delay", m_delay->value() );
    cfg->writeEntry( "Random", m_random->isChecked() );

    int num = m_files->count();
    TQStringList files;
    for( int n=0; n<num; n++ )
        files << m_files->text(n);

    cfg->writeEntry( "Files", files );

    cfg->sync();
}


void SaverConfig::add()
{
    KURL::List files = KFileDialog::getOpenURLs( TQString(), TQString(),
                                                 this, i18n("Select Media Files") );
    for( unsigned int n=0; n<files.count(); n++ )
        m_files->insertItem( files[n].prettyURL(), -1 );
}


void SaverConfig::remove()
{
    int current = m_files->currentItem();
    if( current!=-1 )
        m_files->removeItem( current );
}


void SaverConfig::select()
{
    bool enabled = m_files->currentItem()!=-1;
    m_remove->setEnabled( enabled );
    m_up->setEnabled( enabled && m_files->currentItem()!=0 );
    m_down->setEnabled( enabled && m_files->currentItem()!=(int)m_files->count()-1 );
}


void SaverConfig::up()
{
    int current = m_files->currentItem();
    if ( current>0 ) {
        TQString txt = m_files->currentText();
        m_files->removeItem( current );
        m_files->insertItem( txt, current-1 );
        m_files->setCurrentItem( current-1 );
    }
}


void SaverConfig::down()
{
    int current = m_files->currentItem();
    if ( current!=-1 && current<(int)m_files->count()-1 ) {
        TQString txt = m_files->currentText();
        m_files->removeItem( current );
        m_files->insertItem( txt, current+1 );
        m_files->setCurrentItem( current+1 );
    }
}

#include "tdepartsaver.moc"