knotes
resourcelocal.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <klocale.h>
00035 #include <kmessagebox.h>
00036 #include <kstandarddirs.h>
00037 #include <kconfiggroup.h>
00038
00039 #include <kcal/icalformat.h>
00040
00041 #include "knotes/resourcelocal.h"
00042 #include "knotes/resourcelocalconfig.h"
00043 #include "knotes/resourcemanager.h"
00044 #include "knotes/resourcenotes.h"
00045
00046
00047 ResourceLocal::ResourceLocal()
00048 : ResourceNotes(), mCalendar( QString::fromLatin1( "UTC" ) )
00049 {
00050 kDebug( 5500 ) << "ResourceLocal::ResourceLocal()";
00051 setType( "file" );
00052 mURL = KUrl::fromPath( KGlobal::dirs()->saveLocation( "data", "knotes/" ) +
00053 "notes.ics" );
00054 }
00055
00056 ResourceLocal::ResourceLocal( const KConfigGroup &group )
00057 : ResourceNotes( group ), mCalendar( QString::fromLatin1( "UTC" ) )
00058 {
00059 kDebug( 5500 ) << "ResourceLocal::ResourceLocal()";
00060 setType( "file" );
00061 mURL = KUrl::fromPath( KGlobal::dirs()->saveLocation( "data", "knotes/" ) +
00062 "notes.ics" );
00063
00064 KUrl u = group.readPathEntry( "NotesURL", QString() );
00065 if ( !u.isEmpty() ) {
00066 mURL = u;
00067 }
00068 }
00069
00070 ResourceLocal::~ResourceLocal()
00071 {
00072 }
00073
00074 void ResourceLocal::writeConfig( KConfigGroup &group )
00075 {
00076 KRES::Resource::writeConfig( group );
00077 group.writePathEntry( "NotesURL", mURL.prettyUrl() );
00078 }
00079
00080 bool ResourceLocal::load()
00081 {
00082 mCalendar.load( mURL.path() );
00083
00084 KCal::Journal::List notes = mCalendar.journals();
00085 KCal::Journal::List::ConstIterator it;
00086 for ( it = notes.begin(); it != notes.end(); ++it ) {
00087 manager()->registerNote( this, *it );
00088 }
00089
00090 return true;
00091 }
00092
00093 bool ResourceLocal::save()
00094 {
00095 if ( !mCalendar.save( mURL.path(), new KCal::ICalFormat() ) ) {
00096 KMessageBox::error( 0, i18n( "<qt>Unable to save the notes to <b>%1</b>. "
00097 "Check that there is sufficient disk space."
00098 "<br />There should be a backup in the same "
00099 "directory though.</qt>", mURL.path() ) );
00100 return false;
00101 }
00102
00103 return true;
00104 }
00105
00106 bool ResourceLocal::addNote( KCal::Journal *journal )
00107 {
00108 mCalendar.addJournal( journal );
00109 return true;
00110 }
00111
00112 bool ResourceLocal::deleteNote( KCal::Journal *journal )
00113 {
00114 mCalendar.deleteJournal( journal );
00115 return true;
00116 }
00117
00118 KCal::Alarm::List ResourceLocal::alarms( const KDateTime &from,
00119 const KDateTime &to )
00120 {
00121 KCal::Alarm::List alarms;
00122 KCal::Journal::List notes = mCalendar.journals();
00123 KCal::Journal::List::ConstIterator note;
00124
00125 for ( note = notes.begin(); note != notes.end(); ++note ) {
00126 KDateTime preTime = from.addSecs( -1 );
00127 KCal::Alarm::List::ConstIterator it;
00128 for( it = ( *note )->alarms().begin();
00129 it != ( *note )->alarms().end(); ++it ) {
00130 if ( ( *it )->enabled() ) {
00131 KDateTime dt = ( *it )->nextRepetition( preTime );
00132 if ( dt.isValid() && dt <= to ) {
00133 alarms.append( *it );
00134 }
00135 }
00136 }
00137 }
00138
00139 return alarms;
00140 }