summaryrefslogtreecommitdiff
path: root/core/pim/today/today.cpp
blob: 812f8b5ead6d6cd2bac14c287bdc676e57affec4 (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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
 * today.cpp
 *
 * copyright   : (c) 2002,2003,2004 by Maximilian Reiß
 * email       : harlekin@handhelds.org
 *
 */
/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#define QTOPIA_INTERNAL_LANGLIST

#include "today.h"

#include <qpe/config.h>
#include <qpe/qcopenvelope_qws.h>
#include <qpe/resource.h>
#include <qpe/qpeapplication.h>
#include <qpe/contact.h>

#include <qdir.h>
#include <qtimer.h>
#include <qwhatsthis.h>

struct TodayPlugin {
    TodayPlugin() : library( 0 ), iface( 0 ), guiPart( 0 ), guiBox( 0 ) {}
    QLibrary *library;
    QInterfacePtr<TodayPluginInterface> iface;
    TodayPluginObject *guiPart;
    QWidget *guiBox;
    QString name;
    bool active;
    bool excludeRefresh;
    int pos;
};

static QValueList<TodayPlugin> pluginList;

static  QMap<QString, TodayPlugin> tempList;

Today::Today( QWidget* parent,  const char* name, WFlags fl )
    : TodayBase( parent, name, fl ) {

    QObject::connect( (QObject*)ConfigButton, SIGNAL( clicked() ), this, SLOT( startConfig() ) );
    QObject::connect( (QObject*)OwnerField, SIGNAL( clicked() ), this,  SLOT( editCard() ) );

#if defined(Q_WS_QWS)
#if !defined(QT_NO_COP)
    QCopChannel *todayChannel = new QCopChannel( "QPE/Today" , this );
    connect ( todayChannel, SIGNAL( received(const QCString&,const QByteArray&) ),
              this, SLOT ( channelReceived(const QCString&,const QByteArray&) ) );
#endif
#endif

    setOwnerField();
    m_refreshTimer = new QTimer( this );
    connect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
    m_refreshTimer->start( 15000 );
    m_big_box = 0L;


    layout = new QVBoxLayout( this );
    layout->addWidget( Frame );
    layout->addWidget( OwnerField );

    m_sv = new QScrollView( this );
    m_sv->setResizePolicy( QScrollView::AutoOneFit );
    m_sv->setHScrollBarMode( QScrollView::AlwaysOff );
    m_sv->setFrameShape( QFrame::NoFrame );

    layout->addWidget( m_sv );
    layout->setStretchFactor( m_sv,4 );

    qApp->processEvents();
    loadPlugins();
    QPEApplication::showWidget( this );
}

/**
 * Qcop receive method.
 */
void Today::channelReceived( const QCString &msg, const QByteArray & data ) {
    QDataStream stream( data, IO_ReadOnly );
    if ( msg == "message(QString)" ) {
        QString message;
        stream >> message;
        setOwnerField( message );
    }
}

void Today::setRefreshTimer( int interval ) {

    disconnect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );

    // 0 is "never" case
    if ( !interval == 0 ) {
        connect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
        m_refreshTimer->changeInterval( interval );
    }
}


/**
 * Initialises the owner field with the default value, the username
 */
void Today::setOwnerField() {
    QString file = Global::applicationFileName( "addressbook", "businesscard.vcf" );
    if ( QFile::exists( file ) ) {
        Contact cont = Contact::readVCard( file )[0];
        QString returnString = cont.fullName();
        OwnerField->setText( "<b>" + tr ( "Owned by " ) + returnString + "</b>" );
    } else {
        OwnerField->setText( "<b>" + tr ( "Please fill out the business card" ) + " </b>" );
    }
}

/**
 * Set the owner field with a given QString, for example per qcop.
 */
void Today::setOwnerField( QString &message ) {
    if ( !message.isEmpty() ) {
        OwnerField->setText( "<b>" + message + "</b>" );
    }
}

/**
 * Init stuff needed for today. Reads the config file.
 */
void Today::init() {
    // read config
    Config cfg( "today" );

    cfg.setGroup( "Plugins" );
    m_excludeApplets = cfg.readListEntry( "ExcludeApplets", ',' );
    m_allApplets = cfg.readListEntry( "AllApplets", ',' );

    cfg.setGroup( "General" );
    m_iconSize = cfg.readNumEntry( "IconSize", 18 );
    m_hideBanner = cfg.readNumEntry( "HideBanner", 0 );
    setRefreshTimer( cfg.readNumEntry( "checkinterval", 15000 ) );

    // set the date in top label
    QDate date = QDate::currentDate();
    DateLabel->setText( QString( "<font color=#FFFFFF>" +  TimeString::longDateString( date )  + "</font>" ) );

    if ( m_hideBanner )  {
        Opiezilla->hide();
        TodayLabel->hide();
    } else {
        Opiezilla->show();
        TodayLabel->show();
    }

    if ( m_big_box ) {
        delete m_big_box;
    }

    m_big_box = new QWidget( m_sv->viewport() );
    m_sv->addChild( m_big_box );
    m_bblayout = new QVBoxLayout ( m_big_box );
}

/**
 * Load the plugins
 */
void Today::loadPlugins() {

    init();

    QValueList<TodayPlugin>::Iterator tit;
    if ( !pluginList.isEmpty() ) {
        for ( tit = pluginList.begin(); tit != pluginList.end(); ++tit ) {
            (*tit).guiBox->hide();
            (*tit).guiBox->reparent( 0, QPoint( 0, 0 ) );
            delete (*tit).guiBox;
            (*tit).library->unload();
            delete (*tit).library;
        }
        pluginList.clear();
    }

    QString path = QPEApplication::qpeDir() + "/plugins/today";
#ifdef Q_OS_MACX
    qWarning("Searching for Plugins in: %s", path.latin1());
    QDir dir( path, "lib*.dylib" );
#else
    QDir dir( path, "lib*.so" );
#endif

    QStringList list = dir.entryList();
    QStringList::Iterator it;

    //   QMap<QString, TodayPlugin> tempList;

    for ( it = list.begin(); it != list.end(); ++it ) {
        QInterfacePtr<TodayPluginInterface> iface;
        QLibrary *lib = new QLibrary( path + "/" + *it );

	qDebug( "querying: %s", QString( path + "/" + *it ).latin1() );
        if ( lib->queryInterface( IID_TodayPluginInterface, (QUnknownInterface**)&iface ) == QS_OK ) {
	    qDebug( "accepted: %s", QString( path + "/" + *it ).latin1() );
            qDebug( QString(*it) );

            TodayPlugin plugin;
            plugin.library = lib;
            plugin.iface = iface;
            plugin.name = QString(*it);

            QString type = (*it).left( (*it).find(".") );

            QString lang;
            Config config("locale");
            config.setGroup("Language");
            lang = config.readEntry( "Language", "en" );

            qDebug( "Languages: " + lang );
            QTranslator * trans = new QTranslator( qApp );
            QString tfn = QPEApplication::qpeDir()+"/i18n/" + lang + "/" + type + ".qm";
            if ( trans->load( tfn ) )  {
                qApp->installTranslator( trans );
            }  else {
                delete trans;
            }


            // find out if plugins should be shown
            if ( m_excludeApplets.grep( *it ).isEmpty() ) {
                plugin.active = true;
            } else {
                plugin.active = false;
            }

            plugin.guiPart = plugin.iface->guiPart();
            plugin.excludeRefresh = plugin.guiPart->excludeFromRefresh();

            // package the whole thing into a qwidget so it can be shown and hidden
            plugin.guiBox = new QWidget( m_big_box );
            QHBoxLayout *boxLayout = new QHBoxLayout( plugin.guiBox );
            QPixmap plugPix;
            plugPix.convertFromImage( Resource::loadImage( plugin.guiPart->pixmapNameWidget() ).smoothScale( m_iconSize, m_iconSize ), 0 );
            OClickableLabel* plugIcon = new OClickableLabel( plugin.guiBox );
            plugIcon->setPixmap( plugPix );
            QWhatsThis::add( plugIcon, tr("Click here to launch the associated app") );
            plugIcon->setName( plugin.guiPart->appName() );
            connect( plugIcon, SIGNAL( clicked() ), this, SLOT( startApplication() ) );

            QWidget *plugWidget = plugin.guiPart->widget( plugin.guiBox );
            boxLayout->addWidget( plugIcon, 0, AlignTop );
            boxLayout->addWidget( plugWidget, 0, AlignTop );
            boxLayout->setStretchFactor( plugIcon, 1 );
            boxLayout->setStretchFactor( plugWidget, 9 );

           // "prebuffer" it in one more list, to get the sorting done
            tempList.insert( plugin.name, plugin );

            // on first start the list is off course empty
            if ( m_allApplets.isEmpty() ) {
                pluginList.append( plugin );
                m_bblayout->addWidget( plugin.guiBox );
            }

            // if plugin is not yet in the list, add it to the layout too
            else if ( !m_allApplets.contains( plugin.name ) ) {
                pluginList.append( plugin );
            }
        } else {
            qDebug( "could not recognize %s", QString( path + "/" + *it ).latin1() );
            delete lib;
        }

    }


    if ( !m_allApplets.isEmpty() ) {
        TodayPlugin tempPlugin;
        QStringList::Iterator stringit;

        for( stringit = m_allApplets.begin(); stringit !=  m_allApplets.end(); ++stringit ) {
            tempPlugin = ( tempList.find( *stringit ) ).data();
            if ( !( (tempPlugin.name).isEmpty() ) ) {
                pluginList.append( tempPlugin );
                m_bblayout->addWidget( tempPlugin.guiBox );
            }
        }
    }
    m_bblayout->addStretch( 2 );
    draw();
}




/**
 * Repaint method. Reread all fields.
 */
void Today::draw() {

    if ( pluginList.count() == 0 ) {
        QLabel *noPlugins = new QLabel( this );
        noPlugins->setText( tr( "No plugins found" ) );
        layout->addWidget( noPlugins );
        return;
    }

    uint count = 0;
    TodayPlugin plugin;
    for ( uint i = 0; i < pluginList.count(); i++ ) {
	plugin = pluginList[i];

        if ( plugin.active ) {
            //   qDebug( plugin.name + " is ACTIVE " );
            plugin.guiBox->show();
        } else {
            //  qDebug( plugin.name + " is INACTIVE" );
            plugin.guiBox->hide();
        }
        count++;
    }

    if ( count == 0 ) {
        QLabel *noPluginsActive = new QLabel( this );
        noPluginsActive->setText( tr( "No plugins activated" ) );
        layout->addWidget( noPluginsActive );
    }
    repaint();
}


/**
 * The method for the configuration dialog.
 */
void Today::startConfig() {

    // disconnect timer to prevent problems while being on config dialog
    disconnect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
    m_refreshTimer->stop( );

    TodayConfig conf( this, "dialog", true );

    TodayPlugin plugin;
    QList<TodayConfigWidget> configWidgetList;

    for ( int i = pluginList.count() - 1; i >= 0; i-- ) {
        plugin = pluginList[i];

        // load the config widgets in the tabs
        if ( plugin.guiPart->configWidget( this ) != 0l ) {
            TodayConfigWidget* widget = plugin.guiPart->configWidget( conf.TabWidget3  );
            configWidgetList.append( widget );
            conf.TabWidget3->addTab( widget, plugin.guiPart->pixmapNameConfig()
                                     , plugin.guiPart->appName() );
        }
        // set the order/activate tab
        conf.pluginManagement( plugin.name, plugin.guiPart->pluginName(),
                              Resource::loadPixmap( plugin.guiPart->pixmapNameWidget() ) );
    }

    if ( conf.exec() == QDialog::Accepted ) {
        conf.writeConfig();
        TodayConfigWidget *confWidget;
        for ( confWidget = configWidgetList.first(); confWidget != 0;
              confWidget = configWidgetList.next() ) {
            confWidget->writeConfig();
        }

        // make the plugins to reinitialize ( reread its configs )
        reinitialize();
        draw();

    } else {
        // since refresh is not called in that case , reconnect the signal
        m_refreshTimer->start( 15000 ); // get the config value in here later
        connect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
    }
}



void Today::reinitialize()  {

    Config cfg( "today" );
    cfg.setGroup( "Plugins" );
    m_excludeApplets = cfg.readListEntry( "ExcludeApplets", ',' );
    m_allApplets = cfg.readListEntry( "AllApplets", ',' );

    /* reinitialize all plugins */
    QValueList<TodayPlugin>::Iterator it;
    for ( it = pluginList.begin(); it != pluginList.end(); ++it ) {
        if ( !(*it).excludeRefresh ) {
            (*it).guiPart->reinitialize();
            qDebug( "reinit" );
        }

        /* check if plugins is still to be shown */
        if ( m_excludeApplets.grep( (*it).name ).isEmpty() ) {
            (*it).active = true;
        } else {
            (*it).active = false;
        }

    }

    cfg.setGroup( "General" );
    m_iconSize = cfg.readNumEntry( "IconSize", 18 );
    m_hideBanner = cfg.readNumEntry( "HideBanner", 0 );
    setRefreshTimer( cfg.readNumEntry( "checkinterval", 15000 ) );

    // set the date in top label
    QDate date = QDate::currentDate();
    DateLabel->setText( QString( "<font color=#FFFFFF>" +  TimeString::longDateString( date )  + "</font>" ) );

    if ( m_hideBanner )  {
        Opiezilla->hide();
        TodayLabel->hide();
    } else {
        Opiezilla->show();
        TodayLabel->show();
    }

    delete m_bblayout;
    m_bblayout = new QVBoxLayout( m_big_box );
    TodayPlugin tempPlugin;
    QStringList::Iterator stringit;

    for( stringit = m_allApplets.begin(); stringit !=  m_allApplets.end(); ++stringit ) {
        tempPlugin = ( tempList.find( *stringit ) ).data();
        if ( !( (tempPlugin.name).isEmpty() ) ) {
            m_bblayout->addWidget( tempPlugin.guiBox );
        }
    }
    m_bblayout->addStretch( 2 );

}

/**
 * Refresh for the view. Reload all applets
 *
 */
void Today::refresh() {

    QValueList<TodayPlugin>::Iterator it;
    for ( it = pluginList.begin(); it != pluginList.end(); ++it ) {
        if ( !(*it).excludeRefresh ) {
            (*it).guiPart->refresh();
            qDebug( "refresh" );
        }
    }

    DateLabel->setText( QString( "<font color=#FFFFFF>" +  TimeString::longDateString( QDate::currentDate() )  + "</font>" ) );

    updateGeometry();
    repaint();
}

void Today::startApplication() {
    QCopEnvelope e( "QPE/System", "execute(QString)" );
    e << QString( sender()->name() );
}

/**
 * launch addressbook (personal card)
 */
void Today::editCard() {
    QCopEnvelope env( "QPE/Application/addressbook", "editPersonalAndClose()" );
}

Today::~Today() {
}