knotes

resourcemanager.cpp

00001 /*******************************************************************
00002  This file is part of KNotes.
00003 
00004  Copyright (c) 2004, Bo Thorsen <bo@sonofthor.dk>
00005                2004, Michael Brade <brade@kde.org>
00006 
00007  This program is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  This program is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with this program; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00020  MA  02110-1301, USA.
00021 
00022  In addition, as a special exception, the copyright holders give
00023  permission to link the code of this program with any edition of
00024  the Qt library by Trolltech AS, Norway (or with modified versions
00025  of Qt that use the same license as Qt), and distribute linked
00026  combinations including the two.  You must obey the GNU General
00027  Public License in all respects for all of the code used other than
00028  Qt.  If you modify this file, you may extend this exception to
00029  your version of the file, but you are not obligated to do so.  If
00030  you do not wish to do so, delete this exception statement from
00031  your version.
00032 *******************************************************************/
00033 
00034 #include "knotes/resourcemanager.h"
00035 #include "knotes/resourcelocal.h"
00036 
00037 #include <libkcal/journal.h>
00038 
00039 
00040 KNotesResourceManager::KNotesResourceManager()
00041     : QObject( 0, "KNotes Resource Manager" )
00042 {
00043     m_manager = new KRES::Manager<ResourceNotes>( "notes" );
00044     m_manager->addObserver( this );
00045     m_manager->readConfig();
00046 }
00047 
00048 KNotesResourceManager::~KNotesResourceManager()
00049 {
00050     delete m_manager;
00051 }
00052 
00053 void KNotesResourceManager::load()
00054 {
00055     if ( !m_manager->standardResource() )
00056     {
00057         kdWarning(5500) << "No standard resource yet." << endl;
00058         ResourceNotes *resource = new ResourceLocal( 0 );
00059         m_manager->add( resource );
00060         m_manager->setStandardResource( resource );
00061     }
00062 
00063     // Open all active resources
00064     KRES::Manager<ResourceNotes>::ActiveIterator it;
00065     for ( it = m_manager->activeBegin(); it != m_manager->activeEnd(); ++it )
00066     {
00067         kdDebug(5500) << "Opening resource " + (*it)->resourceName() << endl;
00068         (*it)->setManager( this );
00069         if ( (*it)->open() )
00070             (*it)->load();
00071     }
00072 }
00073 
00074 void KNotesResourceManager::save()
00075 {
00076     KRES::Manager<ResourceNotes>::ActiveIterator it;
00077     for ( it = m_manager->activeBegin(); it != m_manager->activeEnd(); ++it )
00078         (*it)->save();
00079 }
00080 
00081 // when adding a new note, make sure a config file exists!!
00082 
00083 void KNotesResourceManager::addNewNote( KCal::Journal *journal )
00084 {
00085     // TODO: Make this configurable
00086     ResourceNotes *resource = m_manager->standardResource();
00087     if ( resource )
00088     {
00089         resource->addNote( journal );
00090         registerNote( resource, journal );
00091     }
00092     else
00093         kdWarning(5500) << k_funcinfo << "no resource!" << endl;
00094 }
00095 
00096 void KNotesResourceManager::registerNote( ResourceNotes *resource,
00097     KCal::Journal *journal )
00098 {
00099     // TODO: only emit the signal if the journal is new?
00100     m_resourceMap.insert( journal->uid(), resource );
00101     emit sigRegisteredNote( journal );
00102 }
00103 
00104 void KNotesResourceManager::deleteNote( KCal::Journal *journal )
00105 {
00106     QString uid = journal->uid();
00107 
00108     // Remove the journal from the resource it came from
00109     m_resourceMap[ uid ]->deleteNote( journal );
00110     m_resourceMap.remove( uid );
00111 
00112     // libkcal does not delete the journal immediately, therefore it is ok to
00113     // emit the journal here
00114     emit sigDeregisteredNote( journal );
00115 }
00116 
00117 KCal::Alarm::List KNotesResourceManager::alarms( const QDateTime& from, const QDateTime& to )
00118 {
00119     KCal::Alarm::List result;
00120 
00121     KRES::Manager<ResourceNotes>::ActiveIterator it;
00122     for ( it = m_manager->activeBegin(); it != m_manager->activeEnd(); ++it )
00123     {
00124         KCal::Alarm::List list = (*it)->alarms( from, to );
00125         KCal::Alarm::List::Iterator it;
00126         for ( it = list.begin(); it != list.end(); ++it )
00127             result.append( *it );
00128     }
00129 
00130     return result;
00131 }
00132 
00133 void KNotesResourceManager::resourceAdded( ResourceNotes *resource )
00134 {
00135     kdDebug(5500) << "Resource added: " << resource->resourceName() << endl;
00136 
00137     if ( !resource->isActive() )
00138         return;
00139 
00140     resource->setManager( this );
00141     if ( resource->open() )
00142         resource->load();
00143 }
00144 
00145 void KNotesResourceManager::resourceModified( ResourceNotes *resource )
00146 {
00147     kdDebug(5500) << "Resource modified: " << resource->resourceName() << endl;
00148 }
00149 
00150 void KNotesResourceManager::resourceDeleted( ResourceNotes *resource )
00151 {
00152     kdDebug(5500) << "Resource deleted: " << resource->resourceName() << endl;
00153 }
00154 
00155 
00156 #include "resourcemanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys