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

preferences.cpp

Go to the documentation of this file.
00001 #include "preferences.h"
00002 #include <stdlib.h>
00003 
00004 Preferences::Preferences ()
00005   {
00006     db = sqlite_open ( "qmpreferences.db", 0, NULL );
00007   }
00008 
00009 Preferences::~Preferences ()
00010   {
00011     sqlite_close ( db );
00012   }
00013 
00014 void Preferences::addPreferences ()
00015   {
00016     // This function checks the preferences database for existing preferences and adds
00017     // them if they are not there.  First we set up variables.  Preferences are always set
00018     // to non-zero numbers because when we check to see if a preference has been
00019     // added to the database, the result is zero if it hasn't
00020     int rows = 0;
00021     int columns = 0;
00022     char **results;
00023 
00024     sqlite_get_table ( db, "select count() from preferences;", &results, 0, 0, 0 );
00025     if ( atoi ( results [ 1 ] ) != 7 )
00026     {
00027       // dateformat preference 1 = yyyymmdd 2 = yymmdd 3 = mmddyyyy 4 = mmddyy
00028       // 5 = yyyyddmm 6 = yyddmm 7 = ddmmyyyy 8 = ddmmyy
00029       sqlite_get_table ( db, "select preference from preferences where id = 1;", &results, &rows, &columns, 0 );
00030       if ( rows == 0 )
00031         sqlite_exec ( db, "insert into preferences values ( 4, 'dateformat', 0, 0, 0, NULL );", 0, 0, 0 );
00032 
00033       // dateseparator preference 1 = / ( forward slash ) 2 = - ( dash ) 3 = . ( period )
00034       rows = 0;
00035       sqlite_get_table ( db, "select preference from preferences where id = 2;", &results, &rows, &columns, 0 );
00036       if ( rows == 0 )
00037         sqlite_exec ( db, "insert into preferences values ( 1, 'dateseparator', 0, 0, 0, NULL );", 0, 0, 0 );
00038 
00039       // showclearedtransactions preference 0 = no 1 = yes
00040       rows = 0;
00041       sqlite_get_table ( db, "select preference from preferences where id = 3;", &results, &rows, &columns, 0 );
00042       if ( rows == 0 )
00043         sqlite_exec ( db, "insert into preferences values ( 0, 'showclearedtransactions', 0, 0, 0, NULL );", 0, 0, 0 );
00044 
00045       // enable currency support preference 0 = no 1 = yes
00046       rows = 0;
00047       sqlite_get_table ( db, "select preference from preferences where id = 4;", &results, &rows, &columns, 0 );
00048       if ( rows == 0 )
00049         sqlite_exec ( db, "insert into preferences values ( 0, 'enablecurrencysupport', 0, 0, 0, NULL );", 0, 0, 0 );
00050 
00051       // one touch account viewing preference 0 = no 1 = yes
00052       rows = 0;
00053       sqlite_get_table ( db, "select preference from preferences where id = 5;", &results, &rows, &columns, 0 );
00054       if ( rows == 0 )
00055         sqlite_exec ( db, "insert into preferences values ( 0, 'onetouchviewing', 0, 0, 0, NULL );", 0, 0, 0 );
00056 
00057       // exclude transfers from limit view 0 = no 1 = yes
00058       rows = 0;
00059       sqlite_get_table ( db, "select preference from preferences where id = 6;", &results, &rows, &columns, 0 );
00060       if ( rows == 0 )
00061         sqlite_exec ( db, "insert into preferences values ( 0, 'excludetransfersfromlimit', 0, 0, 0, NULL );", 0, 0, 0 );
00062 
00063       // limit number of transactions to 0 = 14 days 1 = 30 days, 2 = 90 days, 3 = 180 days, 4 = 365 days 5 = all
00064       rows = 0;
00065       sqlite_get_table ( db, "select preference from preferences where id = 7;", &results, &rows, &columns, 0 );
00066       if ( rows == 0 )
00067         sqlite_exec ( db, "insert into preferences values ( 0, 'limittransactions', 0, 0, 0, NULL );", 0, 0, 0 );
00068     }
00069   }
00070 
00071 void Preferences::initializeColumnPreferences ()
00072   {
00073     int rows = 0;
00074     int columns = 0;
00075     char **results;
00076 
00077     // initialize accountname column width
00078     sqlite_get_table ( db, "select width from columns where id = 1;", &results, &rows, &columns, 0 );
00079     if ( rows == 0 )
00080       sqlite_exec ( db, "insert into columns values ( 'accountname', 90, 0, 0, 0, NULL );", 0, 0, 0 );
00081 
00082     // initialize accountbalance column width
00083     sqlite_get_table ( db, "select width from columns where id = 2;", &results, &rows, &columns, 0 );
00084     if ( rows == 0 )
00085       sqlite_exec ( db, "insert into columns values ( 'accountbalance', 90, 0, 0, 0, NULL );", 0, 0, 0 );
00086 
00087     // initialize transactiondate column width
00088     sqlite_get_table ( db, "select width from columns where id = 3;", &results, &rows, &columns, 0 );
00089     if ( rows == 0 )
00090       sqlite_exec ( db, "insert into columns values ( 'normaltransactiondate', 50, 0, 0, 0, NULL );", 0, 0, 0 );
00091 
00092     // initialize transactionname column width
00093     sqlite_get_table ( db, "select width from columns where id = 4;", &results, &rows, &columns, 0 );
00094     if ( rows == 0 )
00095       sqlite_exec ( db, "insert into columns values ( 'normaltransactionname', 75, 0, 0, 0, NULL );", 0, 0, 0 );
00096 
00097     // initialize transactionamount column width
00098     sqlite_get_table ( db, "select width from columns where id = 5;", &results, &rows, &columns, 0 );
00099     if ( rows == 0 )
00100       sqlite_exec ( db, "insert into columns values ( 'normaltransactionamount', 50, 0, 0, 0, NULL );", 0, 0, 0 );
00101 
00102     // initialize transactiondate column width
00103     sqlite_get_table ( db, "select width from columns where id = 6;", &results, &rows, &columns, 0 );
00104     if ( rows == 0 )
00105       sqlite_exec ( db, "insert into columns values ( 'extendedtransactiondate', 50, 0, 0, 0, NULL );", 0, 0, 0 );
00106 
00107     // initialize transactionname column width
00108     sqlite_get_table ( db, "select width from columns where id = 7;", &results, &rows, &columns, 0 );
00109     if ( rows == 0 )
00110       sqlite_exec ( db, "insert into columns values ( 'extendedtransactionname', 75, 0, 0, 0, NULL );", 0, 0, 0 );
00111 
00112     // initialize transactionamount column width
00113     sqlite_get_table ( db, "select width from columns where id = 8;", &results, &rows, &columns, 0 );
00114     if ( rows == 0 )
00115       sqlite_exec ( db, "insert into columns values ( 'extendedtransactionamount', 50, 0, 0, 0, NULL );", 0, 0, 0 );
00116 
00117     // initialize transactionaccount column width
00118     sqlite_get_table ( db, "select width from columns where id = 9;", &results, &rows, &columns, 0 );
00119     if ( rows == 0 )
00120       sqlite_exec ( db, "insert into columns values ( 'transactionaccount', 50, 0, 0, 0, NULL );", 0, 0, 0 );
00121 
00122     // initialize account name with currency column width
00123     sqlite_get_table ( db, "select width from columns where id = 10;", &results, &rows, &columns, 0 );
00124     if ( rows == 0 )
00125       sqlite_exec ( db, "insert into columns values ( 'accountnamewithcurrency', 100, 0, 0, 0, NULL );", 0, 0, 0 );
00126 
00127     // initialize account currency column width
00128     sqlite_get_table ( db, "select width from columns where id = 11;", &results, &rows, &columns, 0 );
00129     if ( rows == 0 )
00130       sqlite_exec ( db, "insert into columns values ( 'currencycolumn', 10, 0, 0, 0, NULL );", 0, 0, 0 );
00131 
00132     // initialize account balance with currency column width
00133     sqlite_get_table ( db, "select width from columns where id = 12;", &results, &rows, &columns, 0 );
00134     if ( rows == 0 )
00135       sqlite_exec ( db, "insert into columns values ( 'accountbalancewithcurrency', 50, 0, 0, 0, NULL );", 0, 0, 0 );
00136 
00137     // initialize budget line item column
00138     sqlite_get_table ( db, "select width from columns where id = 13;", &results, &rows, &columns, 0 );
00139     if ( rows == 0 )
00140       sqlite_exec ( db, "insert into columns values ( 'budgetlineitem', 50, 0, 0, 0, NULL );", 0, 0, 0 );
00141 
00142     // initialize budget budget column
00143     sqlite_get_table ( db, "select width from columns where id = 14;", &results, &rows, &columns, 0 );
00144     if ( rows == 0 )
00145       sqlite_exec ( db, "insert into columns values ( 'budgetbudget', 50, 0, 0, 0, NULL );", 0, 0, 0 );
00146 
00147     // initialize budget actual column
00148     sqlite_get_table ( db, "select width from columns where id = 15;", &results, &rows, &columns, 0 );
00149     if ( rows == 0 )
00150       sqlite_exec ( db, "insert into columns values ( 'budgetactual', 50, 0, 0, 0, NULL );", 0, 0, 0 );
00151   }
00152 
00153 void Preferences::initializeSortingPreferences ()
00154   {
00155     int rows = 0;
00156     int columns = 0;
00157     char **results;
00158 
00159     if ( sqlite_get_table ( db, "select count() from sorting;", 0, 0, 0, 0 ) != 0 )
00160       sqlite_exec ( db, "create table sorting ( listbox, column, direction, id integer primary key );", 0, 0, 0 );
00161 
00162     // initialize account listbox sorting. Set direction = 1 here so ascending sort is default.
00163     sqlite_get_table ( db, "select column, direction from sorting where id = 1;", &results, &rows, &columns, 0 );
00164     if ( rows == 0 )
00165       sqlite_exec ( db, "insert into sorting values ( 'accounts', 0, 1, NULL );", 0, 0, 0 );
00166 
00167     // initialize transaction listbox sorting
00168     sqlite_get_table ( db, "select column, direction from sorting where id = 2;", &results, &rows, &columns, 0);
00169     if ( rows == 0 )
00170       sqlite_exec ( db, "insert into sorting values ( 'transactions', 0, 1, NULL );", 0, 0, 0 );
00171 
00172     // initialize budgets listbox sorting
00173     sqlite_get_table ( db, "select column, direction from sorting where id = 3;", & results, &rows, &columns, 0 );
00174     if ( rows == 0 )
00175       sqlite_exec ( db, "insert into sorting values ( 'budgets', 0, 1, NULL );", 0, 0, 0 );
00176   }
00177 
00178 void Preferences::changeColumnPreference ( int id, int width )
00179   {
00180     sqlite_exec_printf ( db, "update columns set width = %i where id = %i;", 0, 0, 0, width, id );
00181   }
00182 
00183 int Preferences::getColumnPreference ( int id )
00184   {
00185     char **results;
00186     sqlite_get_table_printf ( db, "select width from columns where id = %i;", &results, NULL, NULL, NULL, id );
00187     return atoi ( results [ 1 ] );
00188   }
00189 
00190 void Preferences::changeSortingPreference ( int id, int column )
00191   {
00192     int pColumn = 0;    // column setting coming from the prefs object
00193     int pDirection = 0; // direction setting coming from the prefs object
00194 
00195     // because there appears to be no way to query the QT header object directly for it's current sort settings, we have
00196     // to maintain track of them ourselves.  So start by pulling the current saved setting for this view.
00197     getSortingPreference ( id, &pColumn, &pDirection );
00198 
00199     // if the current saved column == the new column, then the user wants to toggle the sort order.
00200     // otherwise we behave like QT does by default, which is to select the new column and default to an ascending sort.
00201     if ( column == pColumn )
00202       sqlite_exec_printf ( db, "update sorting set direction = %i where id = %i;", 0, 0, 0, !pDirection, id );
00203     else
00204       sqlite_exec_printf ( db, "update sorting set column = %i, direction = 1 where id = %i;", 0, 0, 0, column, id );
00205   }
00206 
00207 void Preferences::getSortingPreference ( int id, int *column, int *direction )
00208   {
00209     char **results;
00210     sqlite_get_table_printf ( db, "select column, direction from sorting where id = %i;", &results, NULL, NULL, NULL, id );
00211     *column = atoi ( results [ 2 ] );
00212     *direction = atoi ( results [ 3 ] );
00213   }
00214 
00215 int Preferences::getPreference ( int id )
00216   {
00217     char **results;
00218     sqlite_get_table_printf ( db, "select preference from preferences where id = %i;", &results, NULL, NULL, NULL, id );
00219     return atoi ( results [ 1 ] );
00220   }
00221 
00222 QString Preferences::getSeparator ( )
00223   {
00224     int s = getPreference ( 2 );
00225     if ( s == 1 )
00226       return "/";
00227     if ( s ==2 )
00228       return "-";
00229     else
00230       return ".";
00231   }
00232 
00233 void Preferences::changePreference ( int id, int newpreference )
00234   {
00235     sqlite_exec_printf ( db, "update preferences set preference = %i where id = %i;", 0, 0, 0, newpreference, id );
00236   }
00237 
00238 void Preferences::setDefaultDatePreferences ()
00239   {
00240     sqlite_exec ( db, "update preferences set preference = 4 where id = 1;", 0, 0, 0 );
00241     sqlite_exec ( db, "update preferences set preference = 1 where id = 2;", 0, 0, 0 );
00242   }
00243 
00244 QString Preferences::getDate ( int y, int m, int d )
00245   {
00246     QString date;
00247     int format = getPreference ( 1 );
00248     QString separator = getSeparator();
00249 
00250     // Convert all date integers to QStrings
00251     QString year = QString::number ( y );
00252     QString month = QString::number ( m );
00253     if ( m < 10 )
00254       month.prepend ( "0" );
00255     QString day = QString::number ( d );
00256     if ( d < 10 )
00257       day.prepend ( "0" );
00258 
00259     // Truncate four digit year if necessary
00260     if ( format == 2 || format == 4 || format == 6 || format == 8 )
00261       year.remove ( 0, 2 );
00262 
00263     // Concatenate dates as necessary
00264      if ( format == 1 || format == 2 )
00265       {
00266         date = year;
00267         date.append ( separator );
00268         date.append ( month );
00269         date.append ( separator );
00270         date.append ( day );
00271         return date;
00272       }
00273     if ( format == 3 || format == 4 )
00274       {
00275         date = month;
00276         date.append ( separator );
00277         date.append ( day );
00278         date.append ( separator );
00279         date.append ( year );
00280         return date;
00281       }
00282     if ( format == 5 || format == 6 )
00283       {
00284         date = year;
00285         date.append ( separator );
00286         date.append ( day );
00287         date.append ( separator );
00288         date.append ( month );
00289         return date;
00290       }
00291     if ( format == 7 || format == 8 )
00292       {
00293         date = day;
00294         date.append ( separator );
00295         date.append ( month );
00296         date.append ( separator );
00297         date.append ( year );
00298         return date;
00299       }
00300   }
00301 
00302 QString Preferences::getDate ( int y, int m )
00303   {
00304     QString date;
00305     int format = getPreference ( 1 );
00306     QString separator = getSeparator();
00307 
00308     // Convert all date integers to QStrings
00309     QString year = QString::number ( y );
00310     QString month = QString::number ( m );
00311     if ( m < 10 )
00312       month.prepend ( "0" );
00313 
00314     // Truncate four digit year if necessary
00315     if ( format == 2 || format == 4 || format == 6 || format == 8 )
00316       year.remove ( 0, 2 );
00317 
00318     // Concatenate dates as necessary
00319      if ( format == 1 || format == 2 )
00320       {
00321         date = year;
00322         date.append ( separator );
00323         date.append ( month );
00324         return date;
00325       }
00326     if ( format == 3 || format == 4 )
00327       {
00328         date = month;
00329         date.append ( separator );
00330         date.append ( year );
00331         return date;
00332       }
00333     if ( format == 5 || format == 6 )
00334       {
00335         date = year;
00336         date.append ( separator );
00337         date.append ( month );
00338         return date;
00339       }
00340     if ( format == 7 || format == 8 )
00341       {
00342         date.append ( month );
00343         date.append ( separator );
00344         date.append ( year );
00345         return date;
00346       }
00347   }
00348 
00349 
00350 
00351 
00352 

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