knotes

resourcelocal.cpp

00001 /*******************************************************************
00002  This file is part of KNotes.
00003 
00004  Copyright (c) 2004, Bo Thorsen <bo@sonofthor.dk>
00005                2004-2006, 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 <klocale.h>
00035 #include <kmessagebox.h>
00036 #include <kstandarddirs.h>
00037 
00038 #include <libkcal/icalformat.h>
00039 
00040 #include "knotes/resourcelocal.h"
00041 #include "knotes/resourcelocalconfig.h"
00042 #include "knotes/resourcemanager.h"
00043 #include "knotes/resourcenotes.h"
00044 
00045 
00046 
00047 ResourceLocal::ResourceLocal( const KConfig *config )
00048     : ResourceNotes( config ), mCalendar( QString::fromLatin1( "UTC" ) )
00049 {
00050     kdDebug(5500) << "ResourceLocal::ResourceLocal()" << endl;
00051     setType( "file" );
00052     mURL = KGlobal::dirs()->saveLocation( "data", "knotes/" ) + "notes.ics";
00053 
00054     if ( config )
00055     {
00056         KURL u = config->readPathEntry( "NotesURL" );
00057         if ( !u.isEmpty() )
00058             mURL = u;
00059     }
00060 }
00061 
00062 ResourceLocal::~ResourceLocal()
00063 {
00064 }
00065 
00066 void ResourceLocal::writeConfig( KConfig *config )
00067 {
00068     KRES::Resource::writeConfig( config );
00069     config->writePathEntry( "NotesURL", mURL.prettyURL() );
00070 }
00071 
00072 bool ResourceLocal::load()
00073 {
00074     mCalendar.load( mURL.path() );
00075 
00076     KCal::Journal::List notes = mCalendar.journals();
00077     KCal::Journal::List::ConstIterator it;
00078     for ( it = notes.begin(); it != notes.end(); ++it )
00079         manager()->registerNote( this, *it );
00080 
00081     return true;
00082 }
00083 
00084 bool ResourceLocal::save()
00085 {
00086     if ( !mCalendar.save( mURL.path(), new KCal::ICalFormat() ) )
00087     {
00088         KMessageBox::error( 0,
00089                             i18n("<qt>Unable to save the notes to <b>%1</b>. "
00090                                  "Check that there is sufficient disk space."
00091                                  "<br>There should be a backup in the same directory "
00092                                  "though.</qt>").arg( mURL.path() ) );
00093         return false;
00094     }
00095 
00096     return true;
00097 }
00098 
00099 bool ResourceLocal::addNote( KCal::Journal *journal )
00100 {
00101     mCalendar.addJournal( journal );
00102     return true;
00103 }
00104 
00105 bool ResourceLocal::deleteNote( KCal::Journal *journal )
00106 {
00107     mCalendar.deleteJournal( journal );
00108     return true;
00109 }
00110 
00111 KCal::Alarm::List ResourceLocal::alarms( const QDateTime& from, const QDateTime& to )
00112 {
00113     KCal::Alarm::List alarms;
00114     KCal::Journal::List notes = mCalendar.journals();
00115     KCal::Journal::List::ConstIterator note;
00116     for ( note = notes.begin(); note != notes.end(); ++note )
00117     {
00118         QDateTime preTime = from.addSecs( -1 );
00119         KCal::Alarm::List::ConstIterator it;
00120         for( it = (*note)->alarms().begin(); it != (*note)->alarms().end(); ++it )
00121         {
00122             if ( (*it)->enabled() )
00123             {
00124                 QDateTime dt = (*it)->nextRepetition( preTime );
00125                 if ( dt.isValid() && dt <= to )
00126                     alarms.append( *it );
00127             }
00128         }
00129     }
00130 
00131     return alarms;
00132 }
KDE Home | KDE Accessibility Home | Description of Access Keys