Theme Editor: Stripped out the sub-classes for ProjectModel and turned ProjectModel...
[kugel-rb.git] / utils / themeeditor / projectmodel.cpp
blob925be8195055020266fd64a1ace8a05346cf8488
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 QTextStream fin(&cfg);
44 /* Storing the base directory */
45 QString confDir = config;
46 confDir.chop(confDir.length() - confDir.lastIndexOf('/') - 1);
47 QDir base(confDir);
48 base.cdUp();
49 settings.insert("themebase", base.canonicalPath());
51 while(!fin.atEnd())
53 QString current = fin.readLine();
54 QList<QString> parts = current.split(':');
56 /* A valid setting has at least one : */
57 if(parts.count() < 2)
58 continue;
60 QString setting;
61 for(int i = 1; i < parts.count(); i++)
62 setting.append(parts[i].trimmed());
64 settings.insert(parts[0].trimmed(), setting);
67 cfg.close();
69 /* Adding the files, starting with the .cfg */
70 config.replace(base.canonicalPath() + "/", "");
71 files.append(config);
73 QList<QString> keys;
74 keys.append("wps");
75 keys.append("rwps");
76 keys.append("sbs");
77 keys.append("rsbs");
78 keys.append("fms");
79 keys.append("rfms");
81 for(int i = 0; i < keys.count(); i++)
83 QString file = settings.value(keys[i], "");
84 if(file != "" && file != "-")
86 file.replace("/.rockbox/", "");
87 files.append(file);
94 ProjectModel::~ProjectModel()
98 int ProjectModel::rowCount(const QModelIndex& parent) const
100 return files.count();
103 QVariant ProjectModel::data(const QModelIndex &index, int role) const
105 if(!index.isValid())
106 return QVariant();
108 if(role != Qt::DisplayRole)
109 return QVariant();
111 return files[index.row()];
114 void ProjectModel::activated(const QModelIndex &index)
116 mainWindow->loadTabFromFile(settings.value("themebase", "")
117 + "/" + files[index.row()]);