bae.h 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#ifndef BAE_H
#define BAE_H

#include <QString>
#include <QDebug>
#include <QStandardPaths>
#include <QFileInfo>
#include <QImage>
#include <QTime>
#include <QSettings>
#include <QDirIterator>
12
#include <QScreen>
camilo higuita's avatar
camilo higuita committed
13

Anupam Basak's avatar
Anupam Basak committed
14
#include "vvave_version.h"
15 16

#ifdef STATIC_MAUIKIT
17
#include "fmh.h"
18
#else
19
#include <MauiKit/fmh.h>
20 21
#endif

22 23
namespace BAE
{
24
Q_NAMESPACE
25

26 27
enum SEG
{
28
    HALF = 500,
29 30 31 32 33 34 35 36 37 38 39
    ONE = 1000,
    ONEHALF = 1500,
    TWO = 2000,
    THREE = 3000
};

enum SearchT
{
    LIKE,
    SIMILAR
};
40

41
typedef QMap<BAE::SearchT,QString> SEARCH;
42

43 44 45 46 47
static const SEARCH SearchTMap
{
    { BAE::SearchT::LIKE, "like" },
    { BAE::SearchT::SIMILAR, "similar" }
};
48

49 50 51 52 53 54 55 56 57 58
enum class W : uint_fast8_t
{
    ALL,
    NONE,
    LIKE,
    TAG,
    SIMILAR,
    UNKNOWN,
    DONE,
    DESC,
59 60 61
    ASC,
    CODE,
    MSG
62 63 64 65 66 67 68 69 70 71 72
};

static const QMap<W,QString> SLANG =
{
    {W::ALL, "ALL"},
    {W::NONE, "NONE"},
    {W::LIKE, "LIKE"},
    {W::SIMILAR, "SIMILAR"},
    {W::UNKNOWN, "UNKNOWN"},
    {W::DONE, "DONE"},
    {W::DESC, "DESC"},
73 74 75 76
    {W::ASC, "ASC"},
    {W::TAG, "TAG"},
    {W::MSG, "MSG"},
    {W::CODE, "CODE"}
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
};

enum class TABLE : uint8_t
{
    ALBUMS,
    ARTISTS,
    MOODS,
    PLAYLISTS,
    SOURCES,
    SOURCES_TYPES,
    TRACKS,
    TRACKS_MOODS,
    TRACKS_PLAYLISTS,
    TAGS,
    ALBUMS_TAGS,
    ARTISTS_TAGS,
    TRACKS_TAGS,
    LOGS,
    FOLDERS,
    ALL,
    NONE
};

static const QMap<TABLE,QString> TABLEMAP =
{
    {TABLE::ALBUMS,"albums"},
    {TABLE::ARTISTS,"artists"},
    {TABLE::MOODS,"moods"},
    {TABLE::PLAYLISTS,"playlists"},
    {TABLE::SOURCES,"sources"},
    {TABLE::SOURCES_TYPES,"sources_types"},
    {TABLE::TRACKS,"tracks"},
    {TABLE::TRACKS_MOODS,"tracks_moods"},
    {TABLE::TRACKS_PLAYLISTS,"tracks_playlists"},
    {TABLE::TAGS,"tags"},
    {TABLE::ALBUMS_TAGS,"albums_tags"},
    {TABLE::ARTISTS_TAGS,"artists_tags"},
    {TABLE::TRACKS_TAGS,"tracks_tags"},
    {TABLE::LOGS,"logs"},
    {TABLE::FOLDERS,"folders"}

};

enum class KEY :uint8_t
{
    URL = 0,
    SOURCES_URL = 1,
    TRACK = 2,
    TITLE = 3,
    ARTIST = 4,
    ALBUM = 5,
    DURATION = 6,
    PLAYED = 7,
    BABE = 8,
    STARS = 9,
    RELEASE_DATE = 10,
    ADD_DATE = 11,
    LYRICS = 12,
    GENRE = 13,
    ART = 14,
    TAG = 15,
    MOOD = 16,
    PLAYLIST = 17,
    ARTWORK = 18,
    WIKI = 19,
    SOURCE_TYPE = 20,
    CONTEXT = 21,
    RETRIEVAL_DATE = 22,
    COMMENT = 23,
    ID = 24,
147 148
    SQL = 25,
    NONE = 26
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
};

typedef QMap<BAE::KEY, QString> DB;
typedef QList<DB> DB_LIST;

static const DB KEYMAP =
{
    {KEY::URL, "url"},
    {KEY::SOURCES_URL, "sources_url"},
    {KEY::TRACK, "track"},
    {KEY::TITLE, "title"},
    {KEY::ARTIST, "artist"},
    {KEY::ALBUM, "album"},
    {KEY::DURATION, "duration"},
    {KEY::PLAYED, "played"},
    {KEY::BABE, "babe"},
    {KEY::STARS, "stars"},
    {KEY::RELEASE_DATE, "releaseDate"},
    {KEY::ADD_DATE, "addDate"},
    {KEY::LYRICS, "lyrics"},
    {KEY::GENRE, "genre"},
    {KEY::ART, "art"},
    {KEY::TAG, "tag"},
    {KEY::MOOD, "mood"},
    {KEY::PLAYLIST, "playlist"},
    {KEY::ARTWORK, "artwork"},
    {KEY::WIKI, "wiki"},
    {KEY::SOURCE_TYPE, "source_types_id"},
    {KEY::CONTEXT, "context"},
    {KEY::RETRIEVAL_DATE, "retrieval_date"},
    {KEY::ID, "id"},
180 181
    {KEY::COMMENT, "comment"},
    {KEY::SQL, "sql"}
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
};

static const DB TracksColsMap =
{
    {KEY::URL, KEYMAP[KEY::URL]},
    {KEY::SOURCES_URL, KEYMAP[KEY::SOURCES_URL]},
    {KEY::TRACK, KEYMAP[KEY::TRACK]},
    {KEY::TITLE, KEYMAP[KEY::TITLE]},
    {KEY::ARTIST, KEYMAP[KEY::ARTIST]},
    {KEY::ALBUM, KEYMAP[KEY::ALBUM]},
    {KEY::DURATION, KEYMAP[KEY::DURATION]},
    {KEY::PLAYED, KEYMAP[KEY::PLAYED]},
    {KEY::BABE, KEYMAP[KEY::BABE]},
    {KEY::STARS, KEYMAP[KEY::STARS]},
    {KEY::RELEASE_DATE, KEYMAP[KEY::RELEASE_DATE]},
    {KEY::ADD_DATE, KEYMAP[KEY::ADD_DATE]},
    {KEY::LYRICS, KEYMAP[KEY::LYRICS]},
    {KEY::GENRE, KEYMAP[KEY::GENRE]},
    {KEY::ART, KEYMAP[KEY::ART]}
};

camilo higuita's avatar
camilo higuita committed
203
const static inline QString transformTime(const qint64 &value)
204 205 206
{
    QString tStr;
    if (value)
207
    {
208 209 210 211 212 213 214 215 216 217
        QTime time((value/3600)%60, (value/60)%60, value%60, (value*1000)%1000);
        QString format = "mm:ss";
        if (value > 3600)
            format = "hh:mm:ss";
        tStr = time.toString(format);
    }

    return tStr.isEmpty() ? "00:00" : tStr;
}

camilo higuita's avatar
camilo higuita committed
218
const static inline QString getNameFromLocation(const QString &str)
219 220 221 222 223 224
{
    QString ret;
    int index = 0;

    for(int i = str.size() - 1; i >= 0; i--)
        if(str[i] == '/')
225
        {
226 227
            index = i + 1;
            i = -1;
228 229
        }

230 231
    for(; index < str.size(); index++)
        ret.push_back(str[index]);
232

233 234
    return ret;
}
235

camilo higuita's avatar
camilo higuita committed
236
const static QString SettingPath =  QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)+"/vvave/").toLocalFile();
237
const QString CollectionDBPath =  QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/vvave/").toLocalFile();
camilo higuita's avatar
camilo higuita committed
238 239

#ifdef Q_OS_ANDROID
camilo higuita's avatar
camilo higuita committed
240
const static QString CachePath = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/vvave/").toString();
camilo higuita's avatar
camilo higuita committed
241
#else
camilo higuita's avatar
camilo higuita committed
242
const static QString CachePath =  QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)+"/vvave/").toString();
camilo higuita's avatar
camilo higuita committed
243 244
#endif

camilo higuita's avatar
camilo higuita committed
245 246
const static QString YoutubeCachePath =  QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)+"/vvave/youtube/").toLocalFile();
const static QString NotifyDir = SettingPath;
247

camilo higuita's avatar
camilo higuita committed
248 249 250
const static QString MusicPath = FMH::MusicPath;
const static QString HomePath = FMH::HomePath;
const static QString DownloadsPath = FMH::DownloadsPath;
251

camilo higuita's avatar
camilo higuita committed
252 253
const static QString BabePort = "8483";
const static QString LinkPort = "3333";
254

camilo higuita's avatar
camilo higuita committed
255 256
const static QString appName = QStringLiteral("vvave");
const static QString displayName = QStringLiteral("vvave");
Anupam Basak's avatar
Anupam Basak committed
257
const static QString version = VVAVE_VERSION_STRING;
camilo higuita's avatar
camilo higuita committed
258 259 260
const static QString description = QStringLiteral("Music player");
const static QString orgName = QStringLiteral("Maui");
const static QString orgDomain = QStringLiteral("org.maui.vvave");
camilo higuita's avatar
camilo higuita committed
261

camilo higuita's avatar
camilo higuita committed
262
const static QString DBName = "collection.db";
263

camilo higuita's avatar
camilo higuita committed
264 265
const static QStringList MoodColors = {"#F0FF01","#01FF5B","#3DAEFD","#B401FF","#E91E63"};
const static QStringList defaultSources = QStringList() << BAE::MusicPath
266 267
                                                 << BAE::DownloadsPath
                                                 << BAE::YoutubeCachePath;
268

camilo higuita's avatar
camilo higuita committed
269
const static inline QString fixTitle(const QString &title,const QString &s,const QString &e)
270 271 272 273 274 275 276 277
{
    QString newTitle;
    for(int i=0; i<title.size();i++)
        if(title.at(i)==s)
        {
            while(title.at(i)!=e)
                if(i==title.size()-1) break;
                else i++;
278

279
        }else newTitle+=title.at(i);
280

281 282
    return newTitle.simplified();
}
283

camilo higuita's avatar
camilo higuita committed
284
const static inline QString removeSubstring(const QString &newTitle, const QString &subString)
285 286
{
    const int indexFt = newTitle.indexOf(subString, 0, Qt::CaseInsensitive);
287

288 289 290 291
    if (indexFt != -1)
        return newTitle.left(indexFt).simplified();
    else
        return newTitle;
292
}
293

camilo higuita's avatar
camilo higuita committed
294
const static inline QString ucfirst(const QString &str)/*uppercase first letter*/
295 296
{
    if (str.isEmpty()) return "";
297

298 299 300
    QStringList tokens;
    QStringList result;
    QString output;
301

302
    if(str.contains(" "))
303
    {
304
        tokens = str.split(" ");
305

306
        for(auto str : tokens)
307
        {
308 309 310 311
            str = str.toLower();
            str[0] = str[0].toUpper();
            result<<str;
        }
312

313 314
        output = result.join(" ");
    }else output = str;
315

316 317
    return output.simplified();
}
318

camilo higuita's avatar
camilo higuita committed
319
const static inline QString fixString (const QString &str)
320 321 322 323 324 325 326 327
{
    //title.remove(QRegExp(QString::fromUtf8("[·-`~!@#$%^&*()_—+=|:;<>«»,.?/{}\'\"\\\[\\\]\\\\]")));
    QString title = str;
    title = title.remove(QChar::Null);
    title = title.contains(QChar('\u0000')) ? title.replace(QChar('\u0000'),"") : title;
    title = title.contains("(") && title.contains(")") ? fixTitle(title, "(",")") : title;
    title = title.contains("[") && title.contains("]") ? fixTitle(title, "[","]") : title;
    title = title.contains("{") && title.contains("}") ? fixTitle(title, "{","}") : title;
328 329 330 331 332 333
    title = title.contains("ft", Qt::CaseInsensitive) ? removeSubstring(title, "ft") : title;
    title = title.contains("ft.", Qt::CaseInsensitive) ? removeSubstring(title, "ft.") : title;
    title = title.contains("featuring", Qt::CaseInsensitive) ? removeSubstring(title, "featuring"):title;
    title = title.contains("feat", Qt::CaseInsensitive) ? removeSubstring(title, "feat") : title;
    title = title.contains("official video", Qt::CaseInsensitive) ? removeSubstring(title, "official video"):title;
    title = title.contains("live", Qt::CaseInsensitive) ? removeSubstring(title, "live") : title;
334 335 336 337 338 339 340 341 342 343
    title = title.contains("...") ? title.replace("..." ,"") : title;
    title = title.contains("|") ? title.replace("|", "") : title;
    title = title.contains("|") ? removeSubstring(title, "|") : title;
    title = title.contains('"') ? title.replace('"', "") : title;
    title = title.contains(":") ? title.replace(":", "") : title;
    //    title=title.contains("&")? title.replace("&", "and"):title;
    //qDebug()<<"fixed string:"<<title;

    return ucfirst(title).simplified();
}
344

camilo higuita's avatar
camilo higuita committed
345
 static inline bool fileExists(const QString &url)
346
{
347
    return FMH::fileExists(QUrl::fromLocalFile(url));
348
}
349

camilo higuita's avatar
camilo higuita committed
350
 static inline BAE::TABLE albumType(const FMH::MODEL &albumMap)
351
{
352
    if(albumMap[FMH::MODEL_KEY::ALBUM].isEmpty() && !albumMap[FMH::MODEL_KEY::ARTIST].isEmpty())
353
        return BAE::TABLE::ARTISTS;
354
    else if(!albumMap[FMH::MODEL_KEY::ALBUM].isEmpty() && !albumMap[FMH::MODEL_KEY::ARTIST].isEmpty())
355
        return BAE::TABLE::ALBUMS;
356

357 358
    return BAE::TABLE::NONE;
}
359

camilo higuita's avatar
camilo higuita committed
360
static inline void saveArt(FMH::MODEL &track, const QByteArray &array, const QString &path)
361 362
{
    if(!array.isNull()&&!array.isEmpty())
363
    {
364 365 366 367
        // qDebug()<<"tryna save array: "<< array;

        QImage img;
        img.loadFromData(array);
368
        QString name = !track[FMH::MODEL_KEY::ALBUM].isEmpty() ? track[FMH::MODEL_KEY::ARTIST] + "_" + track[FMH::MODEL_KEY::ALBUM] : track[FMH::MODEL_KEY::ARTIST];
369 370
        name.replace("/", "-");
        name.replace("&", "-");
371 372 373 374
        QString format = "PNG";
        qDebug()<< "SAVER TO "<< path + name + ".png";
        if (img.save(path + name + ".png", format.toLatin1(), 100))
            track.insert(FMH::MODEL_KEY::ARTWORK,path + name + ".png");
375 376 377
        else  qDebug() << "couldn't save artwork";
    }else qDebug()<<"array is empty";
}
378

camilo higuita's avatar
camilo higuita committed
379
static inline void saveSettings(const QString &key, const QVariant &value, const QString &group)
380
{
381
    QSettings setting("vvave","vvave");
382 383 384 385
    setting.beginGroup(group);
    setting.setValue(key,value);
    setting.endGroup();
}
386

camilo higuita's avatar
camilo higuita committed
387
static inline QVariant loadSettings(const QString &key, const QString &group, const QVariant &defaultValue)
388 389
{
    QVariant variant;
390
    QSettings setting("vvave","vvave");
391
    setting.beginGroup(group);
Camilo higuita's avatar
Camilo higuita committed
392
    variant = setting.value(key, defaultValue);
393
    setting.endGroup();
394

395 396
    return variant;
}
397

camilo higuita's avatar
camilo higuita committed
398
static inline bool artworkCache(FMH::MODEL &track, const FMH::MODEL_KEY &type = FMH::MODEL_KEY::ID)
399
{
400
    QDirIterator it(QUrl(CachePath).toLocalFile(), QDir::Files, QDirIterator::NoIteratorFlags);
401
    while (it.hasNext())
402
    {
403 404
        const auto file = QUrl::fromLocalFile(it.next());
        const auto fileName = QFileInfo(file.toLocalFile()).baseName();
405
        switch(type)
406
        {
407
        case FMH::MODEL_KEY::ALBUM:
408
            if(fileName == (track[FMH::MODEL_KEY::ARTIST]+"_"+track[FMH::MODEL_KEY::ALBUM]))
409
            {
410
                track.insert(FMH::MODEL_KEY::ARTWORK, file.toString());
411
                return true;
412
            }
413
            break;
414

415
        case FMH::MODEL_KEY::ARTIST:
416
            if(fileName == (track[FMH::MODEL_KEY::ARTIST]))
417
            {
418
                track.insert(FMH::MODEL_KEY::ARTWORK, file.toString());
419 420 421 422 423
                return true;
            }
            break;
        default: break;
        }
424
    }
425 426
    return false;
}
427

428 429 430 431 432
}



#endif // BAE_H