Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

cfg.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the OPIE Project
00003                =.
00004              .=l.            Copyright (c)  2002 Dan Williams <drw@handhelds.org>
00005            .>+-=
00006  _;:,     .>    :=|.         This file is free software; you can
00007 .> <`_,   >  .   <=          redistribute it and/or modify it under
00008 :`=1 )Y*s>-.--   :           the terms of the GNU General Public
00009 .="- .-=="i,     .._         License as published by the Free Software
00010  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00011      ._= =}       :          or (at your option) any later version.
00012     .%`+i>       _;_.
00013     .i_,=:_.      -<s.       This file is distributed in the hope that
00014      +  .  -:.       =       it will be useful, but WITHOUT ANY WARRANTY;
00015     : ..    .:,     . . .    without even the implied warranty of
00016     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00017   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU General
00018 ..}^=.=       =       ;      Public License for more details.
00019 ++=   -.     .`     .:
00020  :     =  ...= . :.=-        You should have received a copy of the GNU
00021  -.   .:....=;==+<;          General Public License along with this file;
00022   -_. . .   )=.  =           see the file COPYING. If not, write to the
00023     --        :-=`           Free Software Foundation, Inc.,
00024                              59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 
00029 #include <stdio.h>
00030 
00031 #include <qwidget.h>
00032 #include <qpe/config.h>
00033 
00034 #include "cfg.h"
00035 
00036 // --- Cfg --------------------------------------------------------------------
00037 Cfg::Cfg()
00038 {
00039     _currencySymbol="$";
00040     _useSmallFont=TRUE;
00041     _showLocks=FALSE;
00042     _showBalances=FALSE;
00043     _pCategories=new CategoryList();
00044     _bDirty=false;
00045 }
00046 
00047 // --- readStringList ---------------------------------------------------------
00048 // Reads the entries for the control from a configuration file and returns
00049 // them in a StringList. Later this list can be used to create the control. It
00050 // is assumed, that the group is already set. Key is used to enumerate the
00051 // entries.
00052 void Cfg::readStringList(Config &cfg, const char *sKey, QStringList &lst)
00053 {
00054     QString sEntry;
00055     int iCount;
00056 
00057     // read count of elements
00058     sEntry.sprintf("%s_Count", sKey);
00059     iCount=cfg.readNumEntry(sEntry, 0);
00060 
00061     // read entries
00062     for(int i=1; i<=iCount; i++) {
00063         sEntry.sprintf("%s%d", sKey, i);
00064         QString sType=cfg.readEntry(sEntry);
00065         if( sType!=NULL )
00066             lst.append(sType);
00067     }
00068 }
00069 
00070 
00071 // --- readConfig -------------------------------------------------------------
00072 // Reads the member data from the given config file. It will also set the group
00073 // "Config"
00074 void Cfg::readConfig(Config &config)
00075 {
00076     // set group
00077     config.setGroup( "Config" );
00078 
00079     // read scalars
00080         _currencySymbol = config.readEntry( "CurrencySymbol", "$" );
00081         _useSmallFont = config.readBoolEntry( "UseSmallFont", TRUE );
00082         _showLocks = config.readBoolEntry( "ShowLocks", FALSE );
00083         _showBalances = config.readBoolEntry( "ShowBalances", FALSE );
00084     _openLastBook = config.readBoolEntry( "OpenLastBook", FALSE );
00085     _sLastBook = config.readEntry("LastBook", "");
00086     _showLastTab = config.readBoolEntry( "ShowLastTab", FALSE );
00087     _bSavePayees = config.readBoolEntry( "SavePayees", FALSE );
00088 
00089     // Account types
00090     readStringList(config, "AccType", _AccountTypes);
00091     if( _AccountTypes.isEmpty() ) {
00092         _AccountTypes+= (const char *)QWidget::tr("Savings");
00093         _AccountTypes+= (const char *)QWidget::tr("Checking");
00094             _AccountTypes+= (const char *)QWidget::tr("CD");
00095             _AccountTypes+= (const char *)QWidget::tr("Money market");
00096             _AccountTypes+= (const char *)QWidget::tr("Mutual fund");
00097             _AccountTypes+= (const char *)QWidget::tr("Other");
00098         writeStringList(config, "AccType", _AccountTypes);
00099         config.write();
00100     }
00101 
00102     // Payees
00103     readStringList(config, "Payee", _Payees);
00104 
00105     // Read Categories
00106     QStringList lst;
00107     readStringList(config, "Category", lst);
00108     if( lst.isEmpty() ) {
00109         QString type=QWidget::tr("Expense");
00110         lst += QWidget::tr( "Automobile" )+";"+type;
00111             lst += QWidget::tr( "Bills" )+";"+type;
00112             lst += QWidget::tr( "CDs" )+";"+type;
00113             lst += QWidget::tr( "Clothing" )+";"+type;
00114             lst += QWidget::tr( "Computer" )+";"+type;
00115             lst += QWidget::tr( "DVDs" )+";"+type;
00116             lst += QWidget::tr( "Electronics" )+";"+type;
00117             lst += QWidget::tr( "Entertainment" )+";"+type;
00118             lst += QWidget::tr( "Food" )+";"+type;
00119             lst += QWidget::tr( "Gasoline" )+";"+type;
00120             lst += QWidget::tr( "Misc" )+";"+type;
00121             lst += QWidget::tr( "Movies" )+";"+type;
00122             lst += QWidget::tr( "Rent" )+";"+type;
00123             lst += QWidget::tr( "Travel" )+";"+type;
00124 
00125         type=QWidget::tr( "Income" );
00126             lst += QWidget::tr( "Work" )+";"+type;
00127             lst += QWidget::tr( "Family Member" )+";"+type;
00128             lst += QWidget::tr( "Misc. Credit" )+";"+type;
00129 
00130         setCategories(lst);
00131         writeStringList(config, "Category", lst);
00132         config.write();
00133     } else {
00134         setCategories(lst);
00135     }
00136 
00137     // not dirty
00138     _bDirty=false;
00139 }
00140 
00141 
00142 // --- writeStringList --------------------------------------------------------
00143 // Writes the entries in the control in a configuration file. It is assumed,
00144 // that the group is already set. Key is used to enumerate the entries
00145 void Cfg::writeStringList(Config &cfg, const char *sKey, QStringList &lst)
00146 {
00147     QString sEntry;
00148     int iCount=0;
00149     QStringList::Iterator itr;
00150     for(itr=lst.begin(); itr!=lst.end(); itr++) {
00151         sEntry.sprintf("%s%d", sKey, ++iCount);
00152         cfg.writeEntry(sEntry, *itr );
00153     }
00154     sEntry.sprintf("%s_Count", sKey);
00155     cfg.writeEntry(sEntry, iCount);
00156 }
00157 
00158 
00159 // --- writeConfig -----------------------------------------------------------
00160 // Writes all config data back to the config file. The group will be set to
00161 // "Config" and the write be commited
00162 void Cfg::writeConfig(Config &config)
00163 {
00164     // set the group
00165     config.setGroup( "Config" );
00166 
00167     // write scalars
00168     config.writeEntry( "CurrencySymbol", _currencySymbol );
00169     config.writeEntry( "UseSmallFont", _useSmallFont );
00170     config.writeEntry( "ShowLocks", _showLocks );
00171     config.writeEntry( "ShowBalances", _showBalances );
00172     config.writeEntry( "OpenLastBook", _openLastBook );
00173     config.writeEntry( "LastBook", _sLastBook );
00174     config.writeEntry( "ShowLastTab", _showLastTab );
00175     config.writeEntry( "SavePayees", _bSavePayees );
00176 
00177     // write account types
00178     writeStringList(config, "AccType", _AccountTypes);
00179 
00180     // write payees
00181     writeStringList(config, "Payee", _Payees);
00182 
00183     // write categories
00184     QStringList lst=getCategories();
00185     writeStringList(config, "Category", lst );
00186 
00187     // commit write
00188     config.write();
00189     _bDirty=false;
00190 }
00191 
00192 
00193 // --- getCategories ----------------------------------------------------------
00194 QStringList Cfg::getCategories()
00195 {
00196     QStringList ret;
00197     for(Category *itr=_pCategories->first(); itr; itr=_pCategories->next() ) {
00198         QString sEntry;
00199         sEntry.sprintf("%s;%s", (const char *)itr->getName(), (const char *)(itr->isIncome() ? QWidget::tr("Income") : QWidget::tr("Expense")) );
00200         ret.append(sEntry);
00201     }
00202     return(ret);
00203 }
00204 
00205 
00206 // --- setCategories ----------------------------------------------------------
00207 void Cfg::setCategories(QStringList &lst)
00208 {
00209     _pCategories->clear();
00210     QStringList::Iterator itr;
00211     for(itr=lst.begin(); itr!=lst.end(); itr++) {
00212         QStringList split=QStringList::split(";", *itr, true);
00213         if( split.count()<2 ) continue;
00214         bool bIncome= (split[1]==QWidget::tr("Income"));
00215         _pCategories->append( new Category(split[0], bIncome) );
00216     }
00217 }
00218 
00219 
00220 // --- CategoryList ------------------------------------------------------------
00221 CategoryList::CategoryList() : QList<Category>()
00222 {
00223     setAutoDelete(true);
00224 }

Generated on Sat Nov 5 16:16:47 2005 for OPIE by  doxygen 1.4.2