kpilot

memofile.cc

Go to the documentation of this file.
00001 /* memofile.cc          KPilot
00002 **
00003 ** Copyright (C) 2004-2007 by Jason 'vanRijn' Kasper
00004 **
00005 */
00006 
00007 /*
00008 ** This program is free software; you can redistribute it and/or modify
00009 ** it under the terms of the GNU Lesser General Public License as published by
00010 ** the Free Software Foundation; either version 2.1 of the License, or
00011 ** (at your option) any later version.
00012 **
00013 ** This program is distributed in the hope that it will be useful,
00014 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016 ** GNU Lesser General Public License for more details.
00017 **
00018 ** You should have received a copy of the GNU Lesser General Public License
00019 ** along with this program in a file called COPYING; if not, write to
00020 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00021 ** MA 02110-1301, USA.
00022 */
00023 
00024 /*
00025 ** Bug reports and questions can be sent to kde-pim@kde.org
00026 */
00027 
00028 #include "memofile.h"
00029 
00030 Memofile::Memofile(PilotMemo * memo, QString categoryName, QString fileName, QString baseDirectory) :
00031         PilotMemo(memo,memo->text()), _categoryName(categoryName), _filename(fileName),  _baseDirectory(baseDirectory)
00032 {
00033     _lastModified = 0;
00034     _size = 0;
00035     _modified = _modifiedByPalm = false;
00036 }
00037 
00038 Memofile::Memofile(recordid_t id, int category, uint lastModifiedTime, uint size,
00039                    QString categoryName, QString fileName, QString baseDirectory) :
00040         PilotMemo(),  _categoryName(categoryName),
00041         _filename(fileName),_baseDirectory(baseDirectory)
00042 {
00043     setID(id);
00044     PilotRecordBase::setCategory(category);
00045     _lastModified = lastModifiedTime;
00046     _size = size;
00047     _modified = _modifiedByPalm = false;
00048 }
00049 
00050 Memofile::Memofile(int category, QString categoryName, QString fileName, QString baseDirectory) :
00051         PilotMemo(),
00052         _categoryName(categoryName), _filename(fileName), _baseDirectory(baseDirectory)
00053 {
00054     setID(0);
00055     _new = true;
00056     PilotRecordBase::setCategory(category);
00057     _modified = true;
00058     _modifiedByPalm = false;
00059     _lastModified = 0;
00060     _size = 0;
00061 }
00062 
00063 bool Memofile::load()
00064 {
00065     FUNCTIONSETUP;
00066     if (filename().isEmpty()) {
00067         DEBUGKPILOT << fname
00068         << ": I was asked to load, but have no filename to load.  "
00069         << endl;
00070         return false;
00071     }
00072 
00073     QFile f( filenameAbs() );
00074     if ( !f.open( IO_ReadOnly ) ) {
00075         DEBUGKPILOT << fname
00076         << ": Couldn't open file: [" << filenameAbs() << "] to read.  "
00077         << endl;
00078         return false;
00079     }
00080 
00081     QTextStream ts( &f );
00082 
00083     QString text,title,body;
00084     title = filename();
00085     body = ts.read();
00086 
00087     // funky magic.  we want the text of the memofile to have the filename
00088     // as the first line....
00089     if (body.startsWith(title)) {
00090         text = body;
00091     } else {
00092         DEBUGKPILOT << fname
00093         << ": text of your memofile: [" << filename()
00094         << "] didn't include the filename as the first line.  fixing it..." << endl;
00095         text = title + CSL1("\n") + body;
00096     }
00097     
00098     // check length of text.  if it's over the allowable length, warn user.
00099     // NOTE: We don't need to truncate this here, since PilotMemo::setText()
00100     // does it for us.
00101     int _len = text.length();
00102     int _maxlen = PilotMemo::MAX_MEMO_LEN;
00103     if (_len > _maxlen) {
00104         DEBUGKPILOT << fname << ": memofile: [" << filename()
00105                     << "] length: [" << _len << "] is over maximum: ["
00106                     << _maxlen << "] and will be truncated to fit." << endl;
00107     }
00108 
00109     setText(text);
00110     f.close();
00111 
00112     return true;
00113 }
00114 
00115 void Memofile::setID(recordid_t i)
00116 {
00117     if (i != id())
00118         _modifiedByPalm = true;
00119 
00120     PilotMemo::setID(i);
00121 }
00122 
00123 bool Memofile::save()
00124 {
00125     bool result = true;
00126 
00127     if ((isModified() && isLoaded()) || _modifiedByPalm) {
00128         result = saveFile();
00129     }
00130 
00131     return result;
00132 }
00133 
00134 bool Memofile::deleteFile()
00135 {
00136     FUNCTIONSETUP;
00137     DEBUGKPILOT << fname
00138     << ": deleting file: [" << filenameAbs() << "]." << endl;
00139     return QFile::remove(filenameAbs());
00140 
00141 }
00142 
00143 bool Memofile::saveFile()
00144 {
00145     FUNCTIONSETUP;
00146 
00147     if (filename().isEmpty()) {
00148         DEBUGKPILOT << fname
00149         << ": I was asked to save, but have no filename to save to.  "
00150         << endl;
00151         return false;
00152     }
00153 
00154     DEBUGKPILOT << fname
00155     << ": saving memo to file: ["
00156     << filenameAbs() << "]" << endl;
00157 
00158 
00159     QFile f( filenameAbs() );
00160     if ( !f.open( IO_WriteOnly ) ) {
00161         DEBUGKPILOT << fname
00162         << ": Couldn't open file: [" << filenameAbs() << "] to write your memo to.  "
00163         << "This won't end well." << endl;
00164         return false;
00165     }
00166 
00167     QTextStream stream(&f);
00168     stream  << text() << endl;
00169     f.close();
00170 
00171     _lastModified = getFileLastModified();
00172     _size = getFileSize();
00173 
00174     return true;
00175 
00176 }
00177 
00178 bool Memofile::isModified(void)
00179 {
00180     // first, check to see if this file is deleted....
00181     if (!fileExists()) {
00182         return true;
00183     }
00184 
00185     bool modByTimestamp = false;
00186     bool modBySize = false;
00187 
00188     if (_lastModified > 0)
00189         modByTimestamp = isModifiedByTimestamp();
00190 
00191     if (_size > 0)
00192         modBySize = isModifiedBySize();
00193 
00194     bool ret = _modified || modByTimestamp || modBySize;
00195 
00196     return ret;
00197 }
00198 
00199 bool Memofile::isModifiedByTimestamp()
00200 {
00201     if (_lastModified <=0) {
00202         return true;
00203     }
00204 
00205     uint lastModifiedTime = getFileLastModified();
00206     if ( lastModifiedTime != _lastModified) {
00207         return true;
00208     }
00209 
00210     return false;
00211 }
00212 
00213 bool Memofile::isModifiedBySize()
00214 {
00215     if (_size <=0) {
00216         return true;
00217     }
00218 
00219     uint size = getFileSize();
00220     if ( size != _size) {
00221         return true;
00222     }
00223 
00224     return false;
00225 }
00226 
00227 uint Memofile::getFileLastModified()
00228 {
00229     QFileInfo f = QFileInfo(filenameAbs());
00230     uint lastModifiedTime = f.lastModified().toTime_t();
00231     return lastModifiedTime;
00232 }
00233 
00234 uint Memofile::getFileSize()
00235 {
00236     QFileInfo f = QFileInfo(filenameAbs());
00237     uint size = f.size();
00238     return size;
00239 }