fixed compile errors
[maelyrica:maelyrica.git] / lyricsDownloader.cpp
1 /* File: lyricsDownloader.cpp
2  *
3  * This file is part of MaeLyrica.
4  *
5  * Copyright (C) 2012 Marcin Mielniczuk.
6  *
7  * MaeLyrica is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, version 3 of the License.
10  *
11  * MaeLyrica is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  *  along with MaeLyrica.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "lyricsDownloader.h"
21 #include "AZLyricsDownloader.h"
22 #include <curl/curl.h>
23 #include <QDebug>
24 #include <iostream>
25 #include <QFile>
26 #include <QSettings>
27 #include <QtNetwork/QtNetwork>
28
29 using namespace std;
30
31 /////////////////////////////////////////////////////////////////////////////
32
33 size_t lyricsDownloader::write_data_to_var(char *ptr, size_t size, size_t nmemb, void *userdata)
34 {
35     ostringstream * stream = (ostringstream*) userdata;
36     size_t count = size * nmemb;
37     stream->write(ptr, count);
38     return count;
39 }
40
41 /////////////////////////////////////////////////////////////////////////
42
43 Settings lyricsDownloaderManager::progSettings;
44 const QString lyricsDownloaderManager::OMPLyricsRoot("/home/user/.lyrics/");
45
46 lyricsDownloaderManager::lyricsDownloaderManager(LyricsDownloaderClass lyricsDownloaderName)
47 {
48     connect(this, SIGNAL(dataChanged()), this, SLOT(cleanTitle()));
49     connect(this, SIGNAL(dataChanged()), this, SLOT(findSavedLyricsKey())); // recalculate the key, so that it's always up-to-date
50
51     currentClass = lyricsDownloaderName;
52     switch (lyricsDownloaderName)
53     {
54     case AZLyricsDownloaderClass:
55         downloader = new AZLyricsDownloader;
56         break;
57     }
58 }
59
60 lyricsDownloaderManager::~lyricsDownloaderManager()
61 {
62     delete downloader;
63 }
64
65 void lyricsDownloaderManager::changeLyricsDownloader(LyricsDownloaderClass lyricsDownloaderName)
66 {
67     if (lyricsDownloaderName != currentClass) // we don't need to do the same again if it's the same downloader
68     {
69         lyricsDownloader * oldDownloader = downloader;
70         switch (lyricsDownloaderName)
71         {
72         case AZLyricsDownloaderClass:
73             downloader = new AZLyricsDownloader;
74             break;
75         }
76         delete oldDownloader;
77     }
78 }
79
80 short lyricsDownloaderManager::perform()
81 {
82     short ret = downloader -> perform(artist, track);
83     lyrics = downloader -> lyrics_qstr; // clean it too!
84     downloader -> lyrics_qstr.clear();
85     emit lyricsChanged(lyrics);
86     return ret;
87 }
88
89 void lyricsDownloaderManager::cleanTitle()
90 {
91     cleanedTitle = cleanQString( QString::fromStdString(artist));
92     cleanedTitle += "-";
93     cleanedTitle += cleanQString(QString::fromStdString(track));
94 }
95
96 QString lyricsDownloaderManager::cleanQString(const QString qstr)
97 {
98     return qstr.toLower().replace("&", "and").remove(QRegExp("\\([^)]*\\)")).remove(QRegExp("[\\W_]"));
99 }
100
101 bool lyricsDownloaderManager::lyricsExistOMP()
102 {
103     lyricsUrlOMP = OMPLyricsRoot + cleanedTitle + ".txt";
104     return QFile(lyricsUrlOMP).exists();
105 }
106
107 void lyricsDownloaderManager::saveLyricsMaeLyrica()
108 {
109     Settings().setValue("lyrics/" + cleanedTitle, lyrics);
110 }
111
112 bool lyricsDownloaderManager::saveLyricsOMP()
113 {
114     qDebug() << "Saving to " << lyricsUrlOMP << "\n";
115     QFile lyricsFile(lyricsUrlOMP);
116     if (!lyricsFile.open(QIODevice::WriteOnly | QIODevice::Text)) return false;
117     QTextStream lyricsFileStream(&lyricsFile);
118     lyricsFileStream << lyrics;
119     lyricsFile.close();
120     if (lyricsFile.error() != QFile::NoError) return false;
121     else return true;
122 }
123