~ubuntu-branches/debian/sid/kde-style-qtcurve/sid

« back to all changes in this revision

Viewing changes to common/config_file.c

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2009-10-11 14:35:38 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20091011143538-705l6lh4v46hiy5t
Tags: 0.69.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
#define QTC_MAX_FILENAME_LEN   1024
35
35
#define QTC_MAX_INPUT_LINE_LEN 256
36
 
#define QTC_FILE               "qtcurvestylerc"
 
36
#define QTC_FILE               "stylerc"
 
37
#define QTC_OLD_FILE           "qtcurvestylerc"
37
38
#define QTC_VERSION_KEY        "version"
38
39
 
39
40
#ifdef CONFIG_READ
449
450
 
450
451
#endif
451
452
 
452
 
#ifdef CONFIG_WRITE
453
 
#include <kstandarddirs.h>
454
 
#endif
455
 
 
456
453
static const char * getHome()
457
454
{
458
455
    static const char *home=NULL;
478
475
    return home;
479
476
}
480
477
 
481
 
static const char *xdgConfigFolder()
482
 
{
483
 
    static char xdgDir[QTC_MAX_FILENAME_LEN]={'\0'};
484
 
 
485
 
    if(!xdgDir[0])
 
478
#ifdef __cplusplus
 
479
 
 
480
#ifdef QTC_QT_ONLY
 
481
// Take from KStandardDirs::makeDir
 
482
bool makeDir(const QString& dir, int mode)
 
483
{
 
484
    // we want an absolute path
 
485
    if (QDir::isRelativePath(dir))
 
486
        return false;
 
487
 
 
488
#ifdef Q_WS_WIN
 
489
    return QDir().mkpath(dir);
 
490
#else
 
491
    QString target = dir;
 
492
    uint len = target.length();
 
493
 
 
494
    // append trailing slash if missing
 
495
    if (dir.at(len - 1) != '/')
 
496
        target += '/';
 
497
 
 
498
    QString base;
 
499
    uint i = 1;
 
500
 
 
501
    while( i < len )
 
502
    {
 
503
        struct stat st;
 
504
        int pos = target.indexOf('/', i);
 
505
        base += target.mid(i - 1, pos - i + 1);
 
506
        QByteArray baseEncoded = QFile::encodeName(base);
 
507
        // bail out if we encountered a problem
 
508
        if (stat(baseEncoded, &st) != 0)
 
509
        {
 
510
            // Directory does not exist....
 
511
            // Or maybe a dangling symlink ?
 
512
            if (lstat(baseEncoded, &st) == 0)
 
513
                (void)unlink(baseEncoded); // try removing
 
514
 
 
515
            if (mkdir(baseEncoded, static_cast<mode_t>(mode)) != 0) {
 
516
                baseEncoded.prepend( "trying to create local folder " );
 
517
                perror(baseEncoded.constData());
 
518
                return false; // Couldn't create it :-(
 
519
            }
 
520
        }
 
521
        i = pos + 1;
 
522
    }
 
523
    return true;
 
524
#endif
 
525
}
 
526
 
 
527
#else
 
528
#include <kstandarddirs.h>
 
529
#endif
 
530
#endif
 
531
 
 
532
static const char *qtcConfDir()
 
533
{
 
534
    static char *cfgDir=NULL;
 
535
 
 
536
    if(!cfgDir)
486
537
    {
487
538
        static const char *home=NULL;
488
539
 
520
571
            if(!home)
521
572
                home=getHome();
522
573
 
523
 
            sprintf(xdgDir, "%s/.config", home);
 
574
            cfgDir=(char *)malloc(strlen(home)+18);
 
575
            sprintf(cfgDir, "%s/.config/qtcurve/", home);
524
576
        }
525
577
        else
526
 
            strcpy(xdgDir, env);
 
578
        {
 
579
            cfgDir=(char *)malloc(strlen(env)+10);
 
580
            sprintf(cfgDir, "%s/qtcurve/", env);
 
581
        }
527
582
 
528
 
#if defined CONFIG_WRITE || !defined __cplusplus
 
583
//#if defined CONFIG_WRITE || !defined __cplusplus
529
584
        {
530
585
        struct stat info;
531
586
 
532
 
        if(0!=lstat(xdgDir, &info))
 
587
        if(0!=lstat(cfgDir, &info))
533
588
        {
534
589
#ifdef __cplusplus
535
 
            KStandardDirs::makeDir(xdgDir, 0755);
536
 
#else
537
 
            g_mkdir_with_parents(xdgDir, 0755);
538
 
#endif
539
 
        }
540
 
        }
541
 
#endif
542
 
    }
543
 
 
544
 
    return xdgDir;
545
 
}
 
590
#ifdef QTC_QT_ONLY
 
591
            makeDir(cfgDir, 0755);
 
592
#else
 
593
            KStandardDirs::makeDir(cfgDir, 0755);
 
594
#endif
 
595
#else
 
596
            g_mkdir_with_parents(cfgDir, 0755);
 
597
#endif
 
598
        }
 
599
        }
 
600
//#endif
 
601
    }
 
602
 
 
603
    return cfgDir;
 
604
}
 
605
 
 
606
#if !defined QT_VERSION || QT_VERSION >= 0x040000
 
607
 
 
608
#define QTC_MENU_FILE_PREFIX "menubar-"
 
609
 
 
610
#ifdef __cplusplus
 
611
static bool qtcMenuBarHidden(const QString &app)
 
612
{
 
613
    return QFile::exists(QFile::decodeName(qtcConfDir())+QTC_MENU_FILE_PREFIX+app);
 
614
}
 
615
 
 
616
static void qtcSetMenuBarHidden(const QString &app, bool hidden)
 
617
{
 
618
    if(!hidden)
 
619
        QFile::remove(QFile::decodeName(qtcConfDir())+QTC_MENU_FILE_PREFIX+app);
 
620
    else
 
621
        QFile(QFile::decodeName(qtcConfDir())+QTC_MENU_FILE_PREFIX+app).open(QIODevice::WriteOnly);
 
622
}
 
623
#else
 
624
static bool qtcFileExists(const char *name)
 
625
{
 
626
    struct stat info;
 
627
 
 
628
    return 0==lstat(name, &info) && S_ISREG(info.st_mode);
 
629
}
 
630
 
 
631
static char * qtcGetMenuBarFileName(const char *app)
 
632
{
 
633
    char *filename=NULL;
 
634
 
 
635
    if(!filename)
 
636
    {
 
637
        filename=(char *)malloc(strlen(qtcConfDir())+strlen(QTC_MENU_FILE_PREFIX)+strlen(app)+1);
 
638
        sprintf(filename, "%s"QTC_MENU_FILE_PREFIX"%s", qtcConfDir(), app);
 
639
    }
 
640
 
 
641
    return filename;
 
642
}
 
643
 
 
644
static bool qtcMenuBarHidden(const char *app)
 
645
{
 
646
    return qtcFileExists(qtcGetMenuBarFileName(app));
 
647
}
 
648
 
 
649
static void qtcSetMenuBarHidden(const char *app, bool hidden)
 
650
{
 
651
    if(!hidden)
 
652
        unlink(qtcGetMenuBarFileName(app));
 
653
    else
 
654
    {
 
655
        FILE *f=fopen(qtcGetMenuBarFileName(app), "w");
 
656
 
 
657
        if(f)
 
658
            fclose(f);
 
659
    }
 
660
}
 
661
#endif
 
662
#endif
546
663
 
547
664
#ifdef CONFIG_READ
548
665
 
899
1016
#ifdef __cplusplus
900
1017
    if(file.isEmpty())
901
1018
    {
902
 
        const char *xdg=xdgConfigFolder();
 
1019
        const char *cfgDir=qtcConfDir();
903
1020
 
904
 
        if(xdg)
 
1021
        if(cfgDir)
905
1022
        {
906
 
            QString filename(xdg);
 
1023
            QString filename(QFile::decodeName(cfgDir)+QTC_FILE);
907
1024
 
908
 
            filename+="/"QTC_FILE;
 
1025
            if(!QFile::exists(filename))
 
1026
                filename=QFile::decodeName(cfgDir)+"../"QTC_OLD_FILE;
909
1027
            return readConfig(filename, opts, defOpts);
910
1028
        }
911
1029
    }
912
1030
#else
913
1031
    if(!file)
914
1032
    {
915
 
        const char *xdg=xdgConfigFolder();
 
1033
        const char *cfgDir=qtcConfDir();
916
1034
 
917
 
        if(xdg)
 
1035
        if(cfgDir)
918
1036
        {
919
 
            char filename[QTC_MAX_FILENAME_LEN];
 
1037
            char *filename=(char *)malloc(strlen(cfgDir)+strlen(QTC_OLD_FILE)+4);
 
1038
            bool rv=false;
920
1039
 
921
 
            sprintf(filename, "%s/"QTC_FILE, xdg);
922
 
            return readConfig(filename, opts, defOpts);
 
1040
            sprintf(filename, "%s"QTC_FILE, cfgDir);
 
1041
            if(!qtcFileExists(filename))
 
1042
                sprintf(filename, "%s../"QTC_OLD_FILE, cfgDir);
 
1043
            rv=readConfig(filename, opts, defOpts);
 
1044
            free(filename);
 
1045
            return rv;
923
1046
        }
924
1047
    }
925
1048
#endif
950
1073
 
951
1074
            if(opts!=def)
952
1075
                opts->customGradient=def->customGradient;
 
1076
 
 
1077
#if !defined QTC_CONFIG_DIALOG && defined QT_VERSION && (QT_VERSION >= 0x040000)
 
1078
            if(opts!=def)
 
1079
            {
 
1080
                opts->menubarApps=QSet<QString>::fromList(readStringEntry(cfg, "menubarApps").split(','));
 
1081
                opts->menubarApps << "kcalc" << "amarok" << "vlc" << "smplayer";
 
1082
            }
 
1083
#endif
 
1084
 
953
1085
#else
954
1086
            Options newOpts;
955
1087
            Options *def=&newOpts;
1154
1286
            QTC_CFG_READ_BOOL(menuIcons)
1155
1287
            QTC_CFG_READ_BOOL(forceAlternateLvCols)
1156
1288
            QTC_CFG_READ_BOOL(squareLvSelection)
 
1289
            QTC_CFG_READ_BOOL(invertBotTab)
 
1290
            QTC_CFG_READ_BOOL(menubarHiding)
1157
1291
#if defined QTC_CONFIG_DIALOG || (defined QT_VERSION && (QT_VERSION >= 0x040000))
1158
1292
            QTC_CFG_READ_BOOL(stdBtnSizes)
1159
1293
            QTC_CFG_READ_BOOL(titlebarBorder)
1766
1900
    opts->menuIcons=true;
1767
1901
    opts->forceAlternateLvCols=false;
1768
1902
    opts->squareLvSelection=false;
 
1903
    opts->invertBotTab=true;
 
1904
    opts->menubarHiding=false;
1769
1905
#if defined QTC_CONFIG_DIALOG || (defined QT_VERSION && (QT_VERSION >= 0x040000))
1770
1906
    opts->stdBtnSizes=false;
1771
1907
    opts->titlebarBorder=true;
2225
2361
{
2226
2362
    if(!cfg)
2227
2363
    {
2228
 
        const char *xdg=xdgConfigFolder();
 
2364
        const char *cfgDir=qtcConfDir();
2229
2365
 
2230
 
        if(xdg)
 
2366
        if(cfgDir)
2231
2367
        {
2232
 
            char filename[QTC_MAX_FILENAME_LEN];
2233
 
 
2234
 
            sprintf(filename, "%s/"QTC_FILE, xdg);
2235
 
 
2236
2368
#if QT_VERSION >= 0x040000
2237
 
            KConfig defCfg(filename, KConfig::SimpleConfig);
 
2369
            KConfig defCfg(QFile::decodeName(cfgDir)+QTC_FILE, KConfig::SimpleConfig);
2238
2370
#else
2239
 
            KConfig defCfg(filename, false, false);
 
2371
            KConfig defCfg(QFile::decodeName(cfgDir)+QTC_FILE, false, false);
2240
2372
#endif
2241
2373
 
2242
2374
            return writeConfig(&defCfg, opts, def, exportingStyle);
2341
2473
        CFG_WRITE_ENTRY(menuIcons)
2342
2474
        CFG_WRITE_ENTRY(forceAlternateLvCols)
2343
2475
        CFG_WRITE_ENTRY(squareLvSelection)
 
2476
        CFG_WRITE_ENTRY(invertBotTab)
 
2477
        CFG_WRITE_ENTRY(menubarHiding)
2344
2478
#if defined QTC_CONFIG_DIALOG || (defined QT_VERSION && (QT_VERSION >= 0x040000))
2345
2479
        CFG_WRITE_ENTRY(stdBtnSizes)
2346
2480
        CFG_WRITE_ENTRY(titlebarBorder)