Merge branch 'master' of http://git.fredemmott.co.uk/repo/yanihp
[jkt-jerboa.git] / src / core / Util.cpp
blob5b7335d2e91936742f7407e09aa5e911b09fc62e
1 /* LICENSE NOTICE
2 This file is part of Jerboa.
4 Jerboa is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option), version 3 of the license.
9 Jerboa is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with Jerboa. If not, see <http://www.gnu.org/licenses/>.
17 #include "Util.h"
19 #include "config.h"
20 #include "SqlQuery.h"
22 #include <QBuffer>
23 #include <QCoreApplication>
24 #include <QDateTime>
25 #include <QDesktopServices>
26 #include <QDir>
27 #include <QFile>
28 #include <QHttp>
29 #include <QRegExp>
30 #include <QSize>
31 #include <QSqlRecord>
32 #include <QStringList>
33 #include <QVariant>
35 using Jerboa::SqlQuery;
37 namespace Util
39 QString simpleAlbum(QString album)
41 return album.replace(QRegExp("[[(<{].+"), "").trimmed();
44 time_t timestamp()
46 return QDateTime::currentDateTime().toUTC().toTime_t();
49 unsigned int getArtistID(QString artist, QString artistSort)
51 SqlQuery query;
52 query.prepare("SELECT `ID` FROM `Artists` WHERE `Name` = :artist");
53 query.bindValue(":artist", artist);
54 query.exec(); query.first();
55 if ( query.isValid() )
57 // We already have this artist
58 return query.record().value(0).toUInt();
60 else
62 // Need a new artist - firstly, get a romanised form of their name from the sortkey
63 QStringList artistRomanisedBackward = artistSort.split(", ");
64 QStringList artistRomanisedForward;
65 QStringListIterator it(artistRomanisedBackward);
66 it.toBack();
67 while(it.hasPrevious()) artistRomanisedForward.append(it.previous());
68 QString artistRomanised = artistRomanisedForward.join(" ");
70 // Now, insert it into the database
71 query.prepare("INSERT INTO `Artists` (`Name`, `RomanisedName`, `SortKey`) VALUES (:name, :roman, :sort)");
72 query.bindValue(":name", artist);
73 query.bindValue(":roman", artistRomanised);
74 query.bindValue(":sort", artistSort);
75 query.exec();
76 return query.lastInsertId().toUInt();
80 QString artistName(unsigned int id)
82 SqlQuery query;
83 query.prepare("SELECT `Name` FROM `Artists` WHERE `ID` = :id");
84 query.bindValue(":id", id);
85 query.exec(); query.first();
86 if ( query.isValid() )
88 // We already have this artist
89 return query.record().value(0).toString();
91 else
93 return QString::null;
97 QString dataLocation()
99 #ifdef EMBEDDED_USE_FIXED_PATHS
100 return EMBEDDED_FIXED_DATA_PATH;
101 #else
102 return QDesktopServices::storageLocation(QDesktopServices::DataLocation);
103 #endif
106 QString musicLocation()
108 #ifdef EMBEDDED_USE_FIXED_PATHS
109 return EMBEDDED_FIXED_MUSIC_PATH;
110 #else
111 return QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
112 #endif
115 QIcon getIcon(QString name)
117 QIcon icon;
118 QDir images(":/images/");
119 Q_FOREACH(QString item, images.entryList(QDir::AllDirs))
121 QFile file(QString(":/images/%1/%2.png").arg(item, name));
122 if(file.exists())
124 icon.addFile(file.fileName());
126 QFile svgz(QString(":/images/%1/%2.svg").arg(item, name));
127 if(svgz.exists())
129 icon.addFile(svgz.fileName());
132 return icon;
135 QList<QUrl> walkDirectories(const QFileInfo& fileinfo)
137 if (fileinfo.isDir())
139 QList<QUrl> list;
140 QFileInfoList todo = QDir(fileinfo.absoluteFilePath()).entryInfoList(
141 QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files, QDir::Name);
142 Q_FOREACH(QFileInfo info, todo)
144 if (info.isDir())
145 list << walkDirectories(info);
146 else
147 list << QUrl::fromLocalFile(info.absoluteFilePath());
149 return list;
151 else
152 return QList<QUrl>() << QUrl::fromLocalFile(fileinfo.absoluteFilePath());