bae.h 12 KB
Newer Older
Camilo Higuita's avatar
Camilo Higuita committed
1 2 3 4 5 6 7 8 9 10 11 12
#ifndef BAE_H
#define BAE_H

#include "string"
#include <QString>
#include <QDebug>
#include <QStandardPaths>
#include <QFileInfo>
#include <QImage>
#include <QTime>
#include <QSettings>
#include <QDirIterator>
13 14
#include <QApplication>
#include <QScreen>
Camilo Higuita's avatar
Camilo Higuita committed
15 16 17
#include <QtNetwork>
#include <QEventLoop>

Camilo Higuita's avatar
Camilo Higuita committed
18
#include <cmath>
Camilo Higuita's avatar
Camilo Higuita committed
19

20 21 22 23 24

#define BABE_MAJOR_VERSION 0
#define BABE_MINOR_VERSION 1
#define BABE_PATCH_VERSION 0

25
#define BABE_VERSION_STR "0.1.0"
26

27
#define BABE_VERSION KDE_MAKE_VERSION(0,1,0)
28

Camilo Higuita's avatar
Camilo Higuita committed
29 30 31 32
using namespace std;

namespace BAE
{
33
Q_NAMESPACE
Camilo Higuita's avatar
Camilo Higuita committed
34

35 36
inline bool isMobile()
{
37
#if defined(Q_OS_ANDROID)
38
    return true;
39
#elif defined(Q_OS_LINUX)
40
    return false;
41
#elif defined(Q_OS_WIN32)
42
    return false;
43
#elif defined(Q_OS_WIN64)
44
    return false;
45
#elif defined(Q_OS_MACOS)
46
    return false;
47
#elif defined(Q_OS_IOS)
48
    return true;
49
#elif defined(Q_OS_HAIKU)
50
    return false;
51
#endif
52
}
53 54


55 56
enum SEG
{
57
    HALF = 500,
58 59 60 61 62 63 64 65 66 67 68
    ONE = 1000,
    ONEHALF = 1500,
    TWO = 2000,
    THREE = 3000
};

enum SearchT
{
    LIKE,
    SIMILAR
};
Camilo Higuita's avatar
Camilo Higuita committed
69

70
typedef QMap<BAE::SearchT,QString> SEARCH;
Camilo Higuita's avatar
Camilo Higuita committed
71

72 73 74 75 76
static const SEARCH SearchTMap
{
    { BAE::SearchT::LIKE, "like" },
    { BAE::SearchT::SIMILAR, "similar" }
};
Camilo Higuita's avatar
Camilo Higuita committed
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
enum class W : uint_fast8_t
{
    ALL,
    NONE,
    LIKE,
    TAG,
    SIMILAR,
    UNKNOWN,
    DONE,
    DESC,
    ASC
};

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"},
    {W::ASC,"ASC"},
    {W::TAG,"TAG"}
};

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,
172 173
    SQL = 25,
    NONE = 26
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
};

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"},
205 206
    {KEY::COMMENT, "comment"},
    {KEY::SQL, "sql"}
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
};

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]}
};

inline QString transformTime(const qint64 &value)
{
    QString tStr;
    if (value)
Camilo Higuita's avatar
Camilo Higuita committed
232
    {
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        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;
}

inline QString getNameFromLocation(const QString &str)
{
    QString ret;
    int index = 0;

    for(int i = str.size() - 1; i >= 0; i--)
        if(str[i] == '/')
Camilo Higuita's avatar
Camilo Higuita committed
250
        {
251 252
            index = i + 1;
            i = -1;
Camilo Higuita's avatar
Camilo Higuita committed
253 254
        }

255 256
    for(; index < str.size(); index++)
        ret.push_back(str[index]);
Camilo Higuita's avatar
Camilo Higuita committed
257

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    return ret;
}
const QString MusicPath = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
const QStringList MusicPaths = QStandardPaths::standardLocations(QStandardPaths::MusicLocation);
const QString HomePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
const QString SettingPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)+"/babe/";
const QString ArtworkPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/babe/artwork/";
const QString CollectionDBPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/babe/";
const QString CachePath = BAE::isMobile() ? BAE::ArtworkPath : QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)+"/babe/";
const QString YoutubeCachePath = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)+"/babe/youtube/";
const QString DownloadsPath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
const QStringList DownloadsPaths = QStandardPaths::standardLocations(QStandardPaths::DownloadLocation);
const QString NotifyDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
const QString BabePort = "8483";
const QString App = "Babe";
273
const QString Version = BABE_VERSION_STR;
274 275 276 277
const QString DBName = "collection.db";

const QStringList MoodColors = {"#F0FF01","#01FF5B","#3DAEFD","#B401FF","#E91E63"};
const QStringList formats {"*.mp4","*.mp3","*.wav","*.flac","*.ogg","*.m4a"};
278 279 280
const QStringList defaultSources = isMobile() ?
            QStringList()<<BAE::MusicPath<<BAE::DownloadsPath<<BAE::MusicPaths<<BAE::DownloadsPaths :
                           QStringList()<< BAE::MusicPath<<BAE::YoutubeCachePath;
281 282 283 284 285 286 287 288 289 290

inline QString fixTitle(const QString &title,const QString &s,const QString &e)
{
    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++;
Camilo Higuita's avatar
Camilo Higuita committed
291

292
        }else newTitle+=title.at(i);
Camilo Higuita's avatar
Camilo Higuita committed
293

294 295
    return newTitle.simplified();
}
Camilo Higuita's avatar
Camilo Higuita committed
296

297 298 299
inline QString removeSubstring(const QString &newTitle, const QString &subString)
{
    const int indexFt = newTitle.indexOf(subString, 0, Qt::CaseInsensitive);
Camilo Higuita's avatar
Camilo Higuita committed
300

301 302 303
    if (indexFt != -1) return newTitle.left(indexFt).simplified();
    else  return newTitle;
}
Camilo Higuita's avatar
Camilo Higuita committed
304

305 306 307
inline QString ucfirst(const QString &str)/*uppercase first letter*/
{
    if (str.isEmpty()) return "";
Camilo Higuita's avatar
Camilo Higuita committed
308

309 310 311
    QStringList tokens;
    QStringList result;
    QString output;
Camilo Higuita's avatar
Camilo Higuita committed
312

313
    if(str.contains(" "))
Camilo Higuita's avatar
Camilo Higuita committed
314
    {
315
        tokens = str.split(" ");
Camilo Higuita's avatar
Camilo Higuita committed
316

317
        for(auto str : tokens)
Camilo Higuita's avatar
Camilo Higuita committed
318
        {
319 320 321 322
            str = str.toLower();
            str[0] = str[0].toUpper();
            result<<str;
        }
Camilo Higuita's avatar
Camilo Higuita committed
323

324 325
        output = result.join(" ");
    }else output = str;
Camilo Higuita's avatar
Camilo Higuita committed
326

327 328
    return output.simplified();
}
Camilo Higuita's avatar
Camilo Higuita committed
329

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
inline QString fixString (const QString &str)
{
    //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;
    title = title.contains("ft") ? removeSubstring(title, "ft") : title;
    title = title.contains("ft.") ? removeSubstring(title, "ft.") : title;
    title = title.contains("featuring") ? removeSubstring(title, "featuring"):title;
    title = title.contains("feat") ? removeSubstring(title, "feat") : title;
    title = title.contains("official video")?removeSubstring(title, "official video"):title;
    title = title.contains("live") ? removeSubstring(title, "live") : title;
    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();
}
Camilo Higuita's avatar
Camilo Higuita committed
355

356 357 358 359 360 361
inline bool fileExists(const QString &url)
{
    QFileInfo path(url);
    if (path.exists()) return true;
    else return false;
}
Camilo Higuita's avatar
Camilo Higuita committed
362

363 364 365 366 367 368
inline BAE::TABLE albumType(const BAE::DB &albumMap)
{
    if(albumMap[BAE::KEY::ALBUM].isEmpty() && !albumMap[BAE::KEY::ARTIST].isEmpty())
        return BAE::TABLE::ARTISTS;
    else if(!albumMap[BAE::KEY::ALBUM].isEmpty() && !albumMap[BAE::KEY::ARTIST].isEmpty())
        return BAE::TABLE::ALBUMS;
Camilo Higuita's avatar
Camilo Higuita committed
369

370 371
    return BAE::TABLE::NONE;
}
Camilo Higuita's avatar
Camilo Higuita committed
372

373 374 375
inline void saveArt(DB &track, const QByteArray &array, const QString &path)
{
    if(!array.isNull()&&!array.isEmpty())
Camilo Higuita's avatar
Camilo Higuita committed
376
    {
377 378 379 380 381 382 383 384 385 386 387 388 389
        // qDebug()<<"tryna save array: "<< array;

        QImage img;
        img.loadFromData(array);
        QString name = !track[BAE::KEY::ALBUM].isEmpty() ? track[BAE::KEY::ARTIST] + "_" + track[BAE::KEY::ALBUM] : track[BAE::KEY::ARTIST];
        name.replace("/", "-");
        name.replace("&", "-");
        QString format = "JPEG";
        if (img.save(path + name + ".jpg", format.toLatin1(), 100))
            track.insert(BAE::KEY::ARTWORK,path + name + ".jpg");
        else  qDebug() << "couldn't save artwork";
    }else qDebug()<<"array is empty";
}
Camilo Higuita's avatar
Camilo Higuita committed
390

391 392 393 394 395 396 397
inline void saveSettings(const QString &key, const QVariant &value, const QString &group)
{
    QSettings setting("Babe","babe");
    setting.beginGroup(group);
    setting.setValue(key,value);
    setting.endGroup();
}
Camilo Higuita's avatar
Camilo Higuita committed
398

399 400 401 402 403 404 405
inline QVariant loadSettings(const QString &key, const QString &group, const QVariant &defaultValue)
{
    QVariant variant;
    QSettings setting("Babe","babe");
    setting.beginGroup(group);
    variant = setting.value(key,defaultValue);
    setting.endGroup();
Camilo Higuita's avatar
Camilo Higuita committed
406

407 408
    return variant;
}
Camilo Higuita's avatar
Camilo Higuita committed
409

410 411 412 413
inline bool artworkCache(DB &track, const KEY &type = KEY::NONE)
{
    QDirIterator it(CachePath, QDir::Files, QDirIterator::NoIteratorFlags);
    while (it.hasNext())
Camilo Higuita's avatar
Camilo Higuita committed
414
    {
415 416 417
        auto file = it.next();
        auto fileName = QFileInfo(file).fileName();
        switch(type)
Camilo Higuita's avatar
Camilo Higuita committed
418
        {
419 420
        case KEY::ALBUM:
            if(fileName == track[KEY::ARTIST]+"_"+track[KEY::ALBUM]+".jpg")
Camilo Higuita's avatar
Camilo Higuita committed
421
            {
422 423
                track.insert(KEY::ARTWORK,file);
                return true;
Camilo Higuita's avatar
Camilo Higuita committed
424
            }
425
            break;
Camilo Higuita's avatar
Camilo Higuita committed
426

427 428 429 430 431 432 433 434 435
        case KEY::ARTIST:
            if(fileName == track[KEY::ARTIST]+".jpg")
            {
                track.insert(KEY::ARTWORK,file);
                return true;
            }
            break;
        default: break;
        }
Camilo Higuita's avatar
Camilo Higuita committed
436
    }
437 438
    return false;
}
439

440 441 442 443 444 445 446 447 448 449 450 451 452 453
inline bool internetConnection()
{
    QNetworkAccessManager nam;
    QNetworkRequest req(QUrl("http://google.com"));
    QNetworkReply *reply = nam.get(req);
    QEventLoop loop;
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();

    if(reply->bytesAvailable())
        return true;
    else
        return false;
}
Camilo Higuita's avatar
Camilo Higuita committed
454 455 456 457 458
}



#endif // BAE_H