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 #include <opie2/oglobal.h>
00031
00032 #include <qtextstream.h>
00033 #include <qdir.h>
00034 #include <qpe/mimetype.h>
00035 #include <qpe/qpeapplication.h>
00036 #include <qpe/storage.h>
00037
00038 #include <unistd.h>
00039 #include <sys/types.h>
00040
00041 using namespace Opie::Core;
00042
00043 static const char Base64EncMap[64] =
00044 {
00045 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
00046 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
00047 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
00048 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
00049 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
00050 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
00051 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
00052 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F
00053 };
00054
00055 static char Base64DecMap[128] =
00056 {
00057 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00058 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00059 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00060 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00061 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00062 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F,
00063 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
00064 0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00065 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
00066 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
00067 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
00068 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
00069 0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
00070 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
00071 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
00072 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00
00073 };
00074
00075
00076 OConfig* OGlobal::_config = 0;
00077 OConfig* OGlobal::_qpe_config = 0;
00078
00079 void OGlobal::clean_up()
00080 {
00081 qDebug( "Oglobal clean up" );
00082 delete OGlobal::_config;
00083 delete OGlobal::_qpe_config;
00084 OGlobal::_config = 0;
00085 OGlobal::_qpe_config = 0;
00086 }
00087
00088 OConfig* OGlobal::config()
00089 {
00090 if ( !OGlobal::_config )
00091 {
00092
00093 qAddPostRoutine( OGlobal::clean_up );
00094 qDebug( "OGlobal::creating global configuration instance." );
00095 OGlobal::_config = new OConfig( "global" );
00096 }
00097 return OGlobal::_config;
00098 }
00099
00100
00105 Global::Command* OGlobal::builtinCommands() {
00106 return builtin;
00107 }
00108
00109
00113 QGuardedPtr<QWidget>* OGlobal::builtinRunning() {
00114 return running;
00115 }
00116
00117
00125 QString OGlobal::generateUuid() {
00126 QFile file( "/proc/sys/kernel/random/uuid" );
00127 if (!file.open(IO_ReadOnly ) )
00128 return QString::null;
00129
00130 QTextStream stream(&file);
00131
00132 return "{" + stream.read().stripWhiteSpace() + "}";
00133 }
00134
00135
00161
00162
00163
00164 QByteArray OGlobal::encodeBase64(const QByteArray& in, bool insertLFs ) {
00165 if ( in.isEmpty() )
00166 return QByteArray();
00167
00168 unsigned int sidx = 0;
00169 unsigned int didx = 0;
00170 const char* data = in.data();
00171 const unsigned int len = in.size();
00172
00173 unsigned int out_len = ((len+2)/3)*4;
00174
00175
00176
00177
00178 insertLFs = (insertLFs && out_len > 76);
00179 if ( insertLFs )
00180 out_len += ((out_len-1)/76);
00181
00182 int count = 0;
00183 QByteArray out( out_len );
00184
00185
00186 if ( len > 1 )
00187 {
00188 while (sidx < len-2)
00189 {
00190 if ( insertLFs )
00191 {
00192 if ( count && (count%76) == 0 )
00193 out[didx++] = '\n';
00194 count += 4;
00195 }
00196 out[didx++] = Base64EncMap[(data[sidx] >> 2) & 077];
00197 out[didx++] = Base64EncMap[(data[sidx+1] >> 4) & 017 |
00198 (data[sidx] << 4) & 077];
00199 out[didx++] = Base64EncMap[(data[sidx+2] >> 6) & 003 |
00200 (data[sidx+1] << 2) & 077];
00201 out[didx++] = Base64EncMap[data[sidx+2] & 077];
00202 sidx += 3;
00203 }
00204 }
00205
00206 if (sidx < len)
00207 {
00208 if ( insertLFs && (count > 0) && (count%76) == 0 )
00209 out[didx++] = '\n';
00210
00211 out[didx++] = Base64EncMap[(data[sidx] >> 2) & 077];
00212 if (sidx < len-1)
00213 {
00214 out[didx++] = Base64EncMap[(data[sidx+1] >> 4) & 017 |
00215 (data[sidx] << 4) & 077];
00216 out[didx++] = Base64EncMap[(data[sidx+1] << 2) & 077];
00217 }
00218 else
00219 {
00220 out[didx++] = Base64EncMap[(data[sidx] << 4) & 077];
00221 }
00222 }
00223
00224
00225 while (didx < out.size())
00226 {
00227 out[didx] = '=';
00228 didx++;
00229 }
00230
00231 return out;
00232 }
00233
00243 QByteArray OGlobal::decodeBase64( const QByteArray& in) {
00244 if ( in.isEmpty() )
00245 return QByteArray();
00246
00247 QByteArray out;
00248 unsigned int count = 0;
00249 unsigned int len = in.size(), tail = len;
00250 const char* data = in.data();
00251
00252
00253 while ( count < len && (data[count] == '\n' || data[count] == '\r' ||
00254 data[count] == '\t' || data[count] == ' ') )
00255 count++;
00256
00257 if ( strncasecmp(data+count, "begin", 5) == 0 )
00258 {
00259 count += 5;
00260 while ( count < len && data[count] != '\n' && data[count] != '\r' )
00261 count++;
00262
00263 while ( count < len && (data[count] == '\n' || data[count] == '\r') )
00264 count ++;
00265
00266 data += count;
00267 tail = (len -= count);
00268 }
00269
00270
00271
00272 while ( data[tail-1] == '=' || data[tail-1] == '\n' ||
00273 data[tail-1] == '\r' )
00274 if ( data[--tail] != '=' ) len = tail;
00275
00276 unsigned int outIdx = 0;
00277 out.resize( (count=len) );
00278 for (unsigned int idx = 0; idx < count; idx++)
00279 {
00280
00281
00282 unsigned char ch = data[idx];
00283 if ( (ch > 47 && ch < 58) || (ch > 64 && ch < 91 ) ||
00284 (ch > 96 && ch < 123)|| ch == '+' || ch == '/' || ch == '=')
00285 {
00286 out[outIdx++] = Base64DecMap[ch];
00287 }
00288 else
00289 {
00290 len--;
00291 tail--;
00292 }
00293 }
00294
00295
00296
00297
00298 len = (tail>(len/4)) ? tail-(len/4) : 0;
00299 unsigned int sidx = 0, didx = 0;
00300 if ( len > 1 )
00301 {
00302 while (didx < len-2)
00303 {
00304 out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003));
00305 out[didx+1] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017));
00306 out[didx+2] = (((out[sidx+2] << 6) & 255) | (out[sidx+3] & 077));
00307 sidx += 4;
00308 didx += 3;
00309 }
00310 }
00311
00312 if (didx < len)
00313 out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003));
00314
00315 if (++didx < len )
00316 out[didx] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017));
00317
00318
00319 if ( len == 0 || len < out.size() )
00320 out.resize(len);
00321
00322 return out;
00323 }
00324
00325 bool OGlobal::isAppLnkFileName( const QString& str )
00326 {
00327 if (str.isEmpty()||str.at(str.length()-1)==QDir::separator()) return false;
00328 return str.startsWith(MimeType::appsFolderName()+QDir::separator());
00329 }
00330
00331
00332
00333
00334
00335
00336 bool OGlobal::isDocumentFileName( const QString& file )
00337 {
00338 if (file.isEmpty()||file.at(file.length()-1)==QDir::separator()) return false;
00339 if (file.startsWith(QPEApplication::documentDir()+QDir::separator())) return true;
00340 StorageInfo si;
00341 QList< FileSystem > fl = si.fileSystems();
00342 FileSystem*fs;
00343 for (fs = fl.first();fs!=0;fs=fl.next()) {
00344 if (fs->isRemovable()&&file.startsWith(fs->name()+QDir::separator()))
00345 return true;
00346 }
00347 if (file.startsWith(homeDirPath())+"/Documents/") return true;
00348 return false;
00349 }
00350
00351 QString OGlobal::tempDirPath()
00352 {
00353 static QString defstring="/tmp";
00354 char * tmpp = 0;
00355 if ( (tmpp=getenv("TEMP"))) {
00356 return tmpp;
00357 }
00358 return defstring;
00359 }
00360
00361 QString OGlobal::homeDirPath()
00362 {
00363 char * tmpp = getenv("HOME");
00364 return (tmpp?tmpp:"/");
00365 }
00366
00367 bool OGlobal::weekStartsOnMonday()
00368 {
00369 OConfig*conf=OGlobal::qpe_config();
00370 if (!conf)return false;
00371 conf->setGroup("Time");
00372 return conf->readBoolEntry("MONDAY",true);
00373 }
00374
00375 void OGlobal::setWeekStartsOnMonday( bool what)
00376 {
00377 OConfig*conf=OGlobal::qpe_config();
00378 if (!conf)return;
00379 conf->setGroup("Time");
00380 return conf->writeEntry("MONDAY",what);
00381 }
00382
00383 bool OGlobal::useAMPM()
00384 {
00385 OConfig*conf=OGlobal::qpe_config();
00386 if (!conf)return false;
00387 conf->setGroup("Time");
00388 return conf->readBoolEntry("AMPM",false);
00389 }
00390
00391 void OGlobal::setUseAMPM( bool what)
00392 {
00393 OConfig*conf=OGlobal::qpe_config();
00394 if (!conf)return;
00395 conf->setGroup("Time");
00396 return conf->writeEntry("AMPM",what);
00397 }
00398
00399 OConfig* OGlobal::qpe_config()
00400 {
00401 if ( !OGlobal::_qpe_config ) {
00402 OGlobal::_qpe_config = new OConfig( "qpe" );
00403 }
00404 return OGlobal::_qpe_config;
00405 }
00406
00407 bool OGlobal::truncateFile( QFile &f, off_t size )
00408 {
00409
00410
00411
00412 if (!f.exists()||f.size()<(unsigned)size) return false;
00413 bool closeit=false;
00414 if (!f.isOpen()) {
00415 closeit=true;
00416 f.open(IO_Raw | IO_ReadWrite | IO_Append);
00417 }
00418 if (!f.isOpen()) { return false; }
00419 int r = ftruncate(f.handle(),size);
00420 if (closeit) f.close();
00421 return r==0;
00422 }