Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

today.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003 
00004                              Copyright (C) Maximilian Reiss <harlekin@handhelds.org>
00005               =.
00006             .=l.
00007            .>+-=
00008  _;:,     .>    :=|.         This program is free software; you can
00009 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00010 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00011 .="- .-=="i,     .._         License as published by the Free Software
00012  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00013      ._= =}       :          or (at your option) any later version.
00014     .%`+i>       _;_.
00015     .i_,=:_.      -<s.       This program is distributed in the hope that
00016      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00017     : ..    .:,     . . .    without even the implied warranty of
00018     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00019   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00020 ..}^=.=       =       ;      Library General Public License for more
00021 ++=   -.     .`     .:       details.
00022  :     =  ...= . :.=-
00023  -.   .:....=;==+<;          You should have received a copy of the GNU
00024   -_. . .   )=.  =           Library General Public License along with
00025     --        :-=`           this library; see the file COPYING.LIB.
00026                              If not, write to the Free Software Foundation,
00027                              Inc., 59 Temple Place - Suite 330,
00028                              Boston, MA 02111-1307, USA.
00029 */
00030 
00031 #include "today.h"
00032 
00033 #include <opie2/odebug.h>
00034 #include <opie2/opluginloader.h>
00035 #include <opie2/opimcontact.h>
00036 #include <opie2/ocontactaccessbackend_vcard.h>
00037 #include <opie2/ocontactaccess.h>
00038 #include <opie2/oconfig.h>
00039 #include <opie2/oresource.h>
00040 
00041 #include <qpe/qcopenvelope_qws.h>
00042 #include <qpe/qpeapplication.h>
00043 
00044 #include <qdir.h>
00045 #include <qtimer.h>
00046 #include <qwhatsthis.h>
00047 #include <qmessagebox.h>
00048 
00049 using namespace Opie::Ui;
00050 using Opie::Core::OPluginItem;
00051 using Opie::Core::OPluginLoader;
00052 using Opie::Core::OPluginManager;
00053 using Opie::Core::OConfig;
00054 
00055 
00056 struct TodayPlugin {
00057 TodayPlugin() : iface( 0 ), guiPart( 0 ), guiBox( 0 ) {}
00058     QInterfacePtr<TodayPluginInterface> iface;
00059     TodayPluginObject *guiPart;
00060     OPluginItem oplugin;
00061     QWidget *guiBox;
00062     QString name;
00063     bool excludeRefresh;
00064 };
00065 
00066 static  QMap<QString, TodayPlugin> pluginList;
00067 
00068 Today::Today( QWidget* parent,  const char* name, WFlags fl )
00069 : TodayBase( parent, name, fl | WStyle_ContextHelp) {
00070 
00071     setCaption( tr("Today") );
00072     connect( (QObject*)ConfigButton, SIGNAL( clicked() ), this, SLOT( startConfig() ) );
00073     connect( (QObject*)OwnerField, SIGNAL( clicked() ), this,  SLOT( editCard() ) );
00074 
00075 #if !defined(QT_NO_COP)
00076 
00077     QCopChannel *todayChannel = new QCopChannel( "QPE/Today" , this );
00078     connect ( todayChannel, SIGNAL( received(const QCString&,const QByteArray&) ),
00079               this, SLOT ( channelReceived(const QCString&,const QByteArray&) ) );
00080 #endif
00081 
00082     setOwnerField();
00083     m_big_box  = 0l;
00084     m_bblayout = 0l;
00085 
00086     layout = new QVBoxLayout( this );
00087     layout->addWidget( Frame );
00088     layout->addWidget( OwnerField );
00089 
00090 
00091     m_informationLabel = new QLabel( tr("No plugins activated"),  this );
00092     layout->addWidget( m_informationLabel );
00093 
00094     m_sv = new QScrollView( this );
00095     m_sv->setResizePolicy( QScrollView::AutoOneFit );
00096     m_sv->setHScrollBarMode( QScrollView::AlwaysOff );
00097     m_sv->setFrameShape( QFrame::NoFrame );
00098 
00099     layout->addWidget( m_sv );
00100     layout->setStretchFactor( m_sv,4 );
00101 
00102     m_refreshTimer = new QTimer( this );
00103     connect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
00104 
00105     loadPlugins();
00106     loadShellContent();
00107     loadPluginWidgets();
00108 }
00109 
00113 void Today::channelReceived( const QCString &msg, const QByteArray & data ) {
00114     QDataStream stream( data, IO_ReadOnly );
00115     if ( msg == "message(QString)" ) {
00116         QString message;
00117         stream >> message;
00118         setOwnerField( message );
00119     }
00120 }
00121 
00122 void Today::setRefreshTimer( int interval ) {
00123 
00124     disconnect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
00125     // 0 is "never" case
00126     if ( !interval == 0 ) {
00127         connect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
00128         m_refreshTimer->changeInterval( interval );
00129     }
00130 }
00131 
00132 
00136 void Today::setOwnerField() {
00137     QString vfilename = Global::applicationFileName("addressbook", "businesscard.vcf");
00138     Opie::OPimContactAccess acc( "today", vfilename,
00139                                  new Opie::OPimContactAccessBackend_VCard("today", vfilename ) );
00140     if ( acc.load() ) {
00141         Opie::OPimContact cont = acc.allRecords()[0];
00142         QString returnString = cont.fullName();
00143         OwnerField->setText( "<b>" + tr ( "Owned by " ) + returnString + "</b>" );
00144     } else {
00145         OwnerField->setText( "<b>" + tr ( "Please fill out the business card" ) + " </b>" );
00146     }
00147 }
00148 
00152 void Today::setOwnerField( QString &message ) {
00153     if ( !message.isEmpty() ) {
00154         OwnerField->setText( "<b>" + message + "</b>" );
00155     }
00156 }
00157 
00158 
00162 void Today::loadPlugins() {
00163     m_pluginLoader = new OPluginLoader( "today", true );
00164     m_pluginLoader->setAutoDelete( true );
00165 
00166     m_manager = new OPluginManager( m_pluginLoader );
00167     m_manager->load();
00168 }
00169 
00170 void Today::loadShellContent() {
00171     Config cfg( "today" );
00172     cfg.setGroup( "Plugins" );
00173 
00174 
00175     cfg.setGroup( "General" );
00176     m_iconSize = cfg.readNumEntry( "IconSize", 18 );
00177     m_hideBanner = cfg.readNumEntry( "HideBanner", 0 );
00178     setRefreshTimer( cfg.readNumEntry( "checkinterval", 15000 ) );
00179 
00180     // set the date in top label
00181     QDate date = QDate::currentDate();
00182     DateLabel->setText( QString( "<font color=#FFFFFF>" +  TimeString::longDateString( date )  + "</font>" ) );
00183 
00184     if ( m_hideBanner )  {
00185         Opiezilla->hide();
00186         TodayLabel->hide();
00187     } else {
00188         Opiezilla->show();
00189         TodayLabel->show();
00190     }
00191 }
00192 
00193 void Today::loadPluginWidgets() {
00194     /*
00195      * check if loading of Plugins crashed
00196      */
00197     if( m_pluginLoader->isInSafeMode() ) {
00198         QMessageBox::information(this, tr("Today Error"),
00199                                  tr("<qt>The plugin '%1' caused Today to crash."
00200                                     " It could be that the plugin is not properly"
00201                                     " installed.<br>Today tries to continue loading"
00202                                     " plugins.</qt>")
00203                                  .arg( m_manager->crashedPlugin().name()));
00204     }
00205 
00206     OPluginItem::List lst = m_pluginLoader->filtered( true );
00207 
00208     /*
00209      * Show or Hide the information of no plugin installed
00210      */
00211     if ( lst.isEmpty() )
00212         m_informationLabel->show();
00213     else
00214         m_informationLabel->hide();
00215 
00216 
00217     /*
00218      * Now let us add the plugins
00219      */
00220     m_big_box = new QWidget( m_sv->viewport() );
00221     m_sv->addChild( m_big_box );
00222     m_bblayout = new QVBoxLayout( m_big_box );
00223 
00224     for ( OPluginItem::List::Iterator it = lst.begin(); it != lst.end(); ++it ) {
00225         TodayPluginInterface* iface = m_pluginLoader->load<TodayPluginInterface>( *it, IID_TodayPluginInterface );
00226 
00227         TodayPlugin plugin;
00228         plugin.iface = iface;
00229         plugin.name = (*it).name();
00230         plugin.oplugin = (*it);
00231 
00232         plugin.guiPart = plugin.iface->guiPart();
00233         plugin.excludeRefresh = plugin.guiPart->excludeFromRefresh();
00234 
00235         // package the whole thing into a qwidget so it can be shown and hidden
00236         plugin.guiBox = new QWidget( m_big_box );
00237         QHBoxLayout *boxLayout = new QHBoxLayout( plugin.guiBox );
00238         QPixmap plugPix;
00239         plugPix.convertFromImage( Opie::Core::OResource::loadImage( plugin.guiPart->pixmapNameWidget() ).smoothScale( m_iconSize, m_iconSize ), 0 );
00240         OClickableLabel* plugIcon = new OClickableLabel( plugin.guiBox );
00241         plugIcon->setPixmap( plugPix );
00242         QWhatsThis::add
00243             ( plugIcon, tr("Click here to launch the associated app") );
00244         plugIcon->setName( plugin.guiPart->appName() );
00245         connect( plugIcon, SIGNAL( clicked() ), this, SLOT( startApplication() ) );
00246 
00247         QWidget *plugWidget = plugin.guiPart->widget( plugin.guiBox );
00248         boxLayout->addWidget( plugIcon, 0, AlignTop );
00249         boxLayout->addWidget( plugWidget, 0, AlignTop );
00250         boxLayout->setStretchFactor( plugIcon, 1 );
00251         boxLayout->setStretchFactor( plugWidget, 9 );
00252 
00253         pluginList.insert( plugin.name, plugin );
00254         m_bblayout->addWidget(plugin.guiBox);
00255     }
00256 
00257     m_bblayout->addStretch( 1 );
00258     m_big_box->show();
00259 }
00260 
00261 
00265 void Today::startConfig() {
00266     // disconnect timer to prevent problems while being on config dialog
00267     disconnect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
00268     m_refreshTimer->stop( );
00269 
00270     TodayConfig conf( this, "dialog", true );
00271     conf.setUpPlugins( m_manager, m_pluginLoader );
00272 
00273     if ( QPEApplication::execDialog(&conf) == QDialog::Accepted ) {
00274         conf.writeConfig();
00275         clearPluginWidgets();
00276         loadShellContent();
00277         loadPluginWidgets();
00278     } else {
00279         // since reinitialize is not called in that case , reconnect the signal
00280         m_refreshTimer->start( 15000 ); // get the config value in here later
00281         connect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
00282     }
00283 }
00284 
00289 void Today::refresh() {
00290     for ( QMap<QString, TodayPlugin>::Iterator it = pluginList.begin();
00291           it != pluginList.end(); ++it )
00292         it.data().guiPart->refresh();
00293 
00294     DateLabel->setText( QString( "<font color=#FFFFFF>" +  TimeString::longDateString( QDate::currentDate() )  + "</font>" ) );
00295 
00296     updateGeometry();
00297     repaint();
00298 }
00299 
00300 
00301 void Today::startApplication() {
00302     QCopEnvelope e( "QPE/System", "execute(QString)" );
00303     e << QString( sender()->name() );
00304 }
00305 
00306 
00310 void Today::editCard() {
00311     QCopEnvelope env( "QPE/Application/addressbook", "editPersonalAndClose()" );
00312 }
00313 
00314 
00315 Today::~Today() {
00316     clearPluginWidgets();
00317     delete m_pluginLoader;
00318     delete m_manager;
00319 }
00320 
00321 
00322 void Today::clearPluginWidgets() {
00323     for(QMap<QString, TodayPlugin>::Iterator it = pluginList.begin(); it != pluginList.end(); ++it ) {
00324         delete it.data().guiBox;
00325         it.data().guiBox = 0;
00326     }
00327 
00328     pluginList.clear();
00329 
00330     delete m_bblayout;
00331     delete m_big_box;
00332     m_bblayout = 0;
00333     m_big_box  = 0;
00334 }

Generated on Sat Nov 5 16:15:55 2005 for OPIE by  doxygen 1.4.2