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 TQt library by Trolltech AS, Norway (or with modified versions
00025  of TQt that use the same license as TQt), 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  TQt.  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     : TQObject( 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         if ( (*it)->isOpen() ) {
00068             kdDebug(5500) << (*it)->resourceName() << " is already open" << endl;
00069             continue;
00070         }
00071 
00072         kdDebug(5500) << "Opening resource " + (*it)->resourceName() << endl;
00073         (*it)->setManager( this );
00074         if ( (*it)->open() )
00075             (*it)->load();
00076     }
00077 }
00078 
00079 void KNotesResourceManager::save()
00080 {
00081     KRES::Manager<ResourceNotes>::ActiveIterator it;
00082     for ( it = m_manager->activeBegin(); it != m_manager->activeEnd(); ++it )
00083         (*it)->save();
00084 }
00085 
00086 // when adding a new note, make sure a config file exists!!
00087 
00088 bool KNotesResourceManager::addNewNote( KCal::Journal *journal )
00089 {
00090     // TODO: Make this configurable
00091     ResourceNotes *resource = m_manager->standardResource();
00092     if ( resource ) {
00093         if ( resource->addNote( journal ) ) {
00094             registerNote( resource, journal );
00095             return true;
00096         }
00097     } else {
00098         kdWarning(5500) << k_funcinfo << "no resource!" << endl;
00099     }
00100     return false;
00101 }
00102 
00103 void KNotesResourceManager::registerNote( ResourceNotes *resource,
00104     KCal::Journal *journal )
00105 {
00106     // TODO: only emit the signal if the journal is new?
00107     m_resourceMap.insert( journal->uid(), resource );
00108     emit sigRegisteredNote( journal );
00109 }
00110 
00111 void KNotesResourceManager::deleteNote( KCal::Journal *journal )
00112 {
00113     if ( !journal )
00114         return;
00115 
00116     TQString uid = journal->uid();
00117 
00118     // Remove the journal from the resource it came from
00119     ResourceNotes *res = m_resourceMap[ uid ];
00120     if ( res ) {
00121         res->deleteNote( journal );
00122         m_resourceMap.remove( uid );
00123 
00124         // libkcal does not delete the journal immediately, therefore it is ok to
00125         // emit the journal here
00126         emit sigDeregisteredNote( journal );
00127     }
00128 }
00129 
00130 KCal::Alarm::List KNotesResourceManager::alarms( const TQDateTime& from, const TQDateTime& to )
00131 {
00132     KCal::Alarm::List result;
00133 
00134     KRES::Manager<ResourceNotes>::ActiveIterator it;
00135     for ( it = m_manager->activeBegin(); it != m_manager->activeEnd(); ++it )
00136     {
00137         KCal::Alarm::List list = (*it)->alarms( from, to );
00138         KCal::Alarm::List::ConstIterator it;
00139         for ( it = list.constBegin(); it != list.constEnd(); ++it )
00140             result.append( *it );
00141     }
00142 
00143     return result;
00144 }
00145 
00146 void KNotesResourceManager::resourceAdded( ResourceNotes *resource )
00147 {
00148     kdDebug(5500) << "Resource added: " << resource->resourceName() << endl;
00149 
00150     if ( !resource->isActive() )
00151         return;
00152 
00153     if ( resource->isOpen() ) {
00154         kdDebug(5500) << resource->resourceName() << " is already open" << endl;
00155         return;
00156     }
00157 
00158     resource->setManager( this );
00159     if ( resource->open() )
00160         resource->load();
00161 }
00162 
00163 void KNotesResourceManager::resourceModified( ResourceNotes *resource )
00164 {
00165     kdDebug(5500) << "Resource modified: " << resource->resourceName() << endl;
00166 }
00167 
00168 void KNotesResourceManager::resourceDeleted( ResourceNotes *resource )
00169 {
00170     kdDebug(5500) << "Resource deleted: " << resource->resourceName() << endl;
00171 }
00172 
00173 
00174 #include "resourcemanager.moc"