Proper initialization of static variables in codeclib.
[kugel-rb.git] / utils / themeeditor / models / projectmodel.cpp
blob2663e8b42690f27d9d95cabd86e53899b3d1eea9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Robert Bieber
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include "projectmodel.h"
24 #include "editorwindow.h"
26 #include <QFile>
27 #include <QTextStream>
28 #include <QMap>
29 #include <QDir>
31 ProjectModel::ProjectModel(QString config, EditorWindow* mainWindow,
32 QObject *parent)
33 : QAbstractListModel(parent),
34 mainWindow(mainWindow)
36 /* Reading the config file */
37 QFile cfg(config);
38 cfg.open(QFile::ReadOnly | QFile::Text);
39 if(!cfg.isReadable())
40 return;
42 settings.insert("configfile", config);
44 QTextStream fin(&cfg);
46 /* Storing the base directory */
47 QString confDir = config;
48 confDir.chop(confDir.length() - confDir.lastIndexOf('/') - 1);
49 QDir base(confDir);
50 base.cdUp();
51 settings.insert("themebase", base.canonicalPath());
53 while(!fin.atEnd())
55 QString current = fin.readLine();
56 QList<QString> parts = current.split(':');
58 /* A valid setting has at least one : */
59 if(parts.count() < 2)
60 continue;
62 QString setting;
63 for(int i = 1; i < parts.count(); i++)
64 setting.append(parts[i].trimmed());
66 settings.insert(parts[0].trimmed(), setting);
69 cfg.close();
71 /* Adding the files, starting with the .cfg */
72 config.replace(base.canonicalPath() + "/", "");
73 files.append(config);
75 QList<QString> keys;
76 keys.append("wps");
77 keys.append("rwps");
78 keys.append("sbs");
79 keys.append("rsbs");
80 keys.append("fms");
81 keys.append("rfms");
83 for(int i = 0; i < keys.count(); i++)
85 QString file = settings.value(keys[i], "");
86 if(file != "" && file != "-")
88 file.replace("/.rockbox/", "");
89 files.append(file);
96 ProjectModel::~ProjectModel()
100 int ProjectModel::rowCount(const QModelIndex& parent) const
102 return files.count();
105 QVariant ProjectModel::data(const QModelIndex &index, int role) const
107 if(!index.isValid())
108 return QVariant();
110 if(role != Qt::DisplayRole)
111 return QVariant();
113 return files[index.row()];
116 void ProjectModel::activated(const QModelIndex &index)
118 if(index.row() == 0)
120 ConfigDocument* doc = new ConfigDocument(settings,
121 settings.value("themebase",
122 "") + "/" +
123 files[index.row()]);
124 QObject::connect(doc, SIGNAL(configFileChanged(QString)),
125 mainWindow, SLOT(configFileChanged(QString)));
126 mainWindow->loadConfigTab(doc);
128 else
130 mainWindow->loadTabFromSkinFile(settings.value("themebase", "")
131 + "/" + files[index.row()]);