Switch to QtNetwork
[maelyrica:maelyrica.git] / qml / maelyrica / MainPage.qml
1 import QtQuick 1.1
2 import com.nokia.meego 1.0
3 import com.nokia.extras 1.0
4 import MaeLyrica 1.0
5
6 Page
7 {
8   //  property int items:  5
9    // property int itemheight: screenheight / items - screenheight
10     property int items: 4
11     property int heightForSpacer: (height - caption.height) / items
12     property string searchString
13     property bool readingSaved:  false
14     id: mainPage
15     tools: maintoolbar
16
17     Loader
18     {
19         id: goLoader
20         onLoaded: console.log("Show lyrics page loaded")
21     }
22     Loader
23     {
24         id: selectArtistLoader
25         onLoaded: console.log("Select artist page loaded")
26     }
27     Loader
28     {
29         id: editTrackDataSheetLoader
30         onLoaded: console.log("Edit track data sheet loaded")
31     }
32     Loader
33     {
34         id: recentSearchesSheetLoader
35         onLoaded: console.log("Recent searches sheet loaded")
36     }
37     Loader
38     {
39         id: settingsSheetLoader
40         onLoaded: console.log("Settings sheet loaded")
41     }
42
43
44     Column
45     {
46         width: parent.width
47         MaeLyricaHeader
48         {
49             id: caption
50             headertext: "Search for lyrics"
51         }
52
53         Item
54         {
55             height: heightForSpacer
56             width: parent.width
57             CheckBox
58             {
59                 id: tracksrc
60                 text: "Select track from library" // label isn't needed with PageStackWindow
61                 checked: true
62                 anchors.centerIn: parent
63             }
64         }
65         // button for selecting a track from the media library
66         Item
67         {
68             height: heightForSpacer
69             width: parent.width
70             Row
71             {
72                 anchors.centerIn: parent
73                 width: parent.width
74                 RowSpacer
75                 {
76                     id: spacer1
77                     width: 10 + lyricssrctext.width - selectedtracktext.width // 10 + how much wider is the 2nd text
78                 }
79
80                 Label
81                 {
82                     id: selectedtracktext
83                     anchors.verticalCenter: selecttrack.verticalCenter
84                     text: "Selected track: "
85                 }
86
87                 Button
88                 {
89                     id: selecttrack
90                     text: "<i>No track selected</i>"
91                     // 2 times margin - on the left and right (the same as 2nd button's left spacer)
92                     width: parent.width - selectedtracktext.width - spacer1.width - spacer2.width
93                     onClicked:
94                     {
95                         if ( tracksrc.checked  )
96                         {
97                             selectArtistLoader.source = "SelectArtistPage.qml"
98                             pageStack.push(selectArtistLoader.item)
99                         }
100                         else
101                         {
102                             editTrackDataSheetLoader.source = "EditTrackDataSheet.qml"
103                             editTrackDataSheetLoader.item.open()
104                         }
105                     }
106                 }
107             }
108         }
109
110         //line edits for entering the data manually
111
112         Item
113         {
114             height: heightForSpacer
115             width: parent.width
116             Row
117             {
118                 anchors.centerIn: parent
119                 width: parent.width
120                 RowSpacer
121                 {
122                     id: spacer2
123                     width: 10
124                 }
125
126
127                 Label
128                 {
129                     id: lyricssrctext
130                     anchors.verticalCenter: lyricssrcbutton.verticalCenter
131                     text: "Download source: "
132                 }
133
134                 Button
135                 {
136                     id: lyricssrcbutton
137                     text: lyricssrcdialog.model.get(lyricssrcdialog.selectedIndex).name
138                     width: parent.width - 3 * spacer2.width - lyricssrctext.width
139                     onClicked: { lyricssrcdialog.open(); }
140                 }
141             }
142         }
143
144         Item
145         {
146             height: heightForSpacer
147             width: parent.width
148             Button
149             {
150                 anchors.centerIn: parent
151                 id: go
152                 text: "Go!"
153                 width: parent.width
154                 onClicked:
155                 {
156                     if ( downloader.anyFieldEmpty() )
157                     {
158                         emptyfieldsdialogmain.open()
159                     }
160                     else
161                     {
162                         goLoader.source = "ShowLyricsPage.qml" // we need to load it now so that loadSavedLyricsDialog can do its work
163                         if ( downloader.theseLyricsSaved && programSettings.autoScanSaves )
164                         {
165                             console.log("these lyrics saved")
166                             loadSavedLyricsDialog.open()
167                         }
168                         else
169                         {
170                             console.log("these lyrics not saved")
171                             performDownload()
172                         }
173
174                     }
175                 }
176             }
177         }
178     }
179     ToolBarLayout
180     {
181         id: maintoolbar
182         ToolIcon
183         {
184             iconId: "toolbar-back"
185             onClicked: quitdialog.open()
186         }
187         ToolIcon
188         {
189             iconId: "toolbar-view-menu"
190             onClicked: (mainMenu.status == DialogStatus.Closed) ? mainMenu.open() : mainMenu.close()
191         }
192     }
193     Menu
194     {
195         id: mainMenu
196         visualParent: pageStack
197         MenuLayout
198         {
199             MenuItem
200             {
201                 text: "Recent searches"
202                 onClicked:
203                 {
204                     recentSearchesSheetLoader.source = "RecentSearchesSheet.qml"
205                     recentSearchesSheetLoader.item.open()
206                 }
207             }
208             MenuItem
209             {
210                 text: "Settings"
211                 onClicked:
212                 {
213                     settingsSheetLoader.source = "SettingsSheet.qml"
214                     settingsSheetLoader.item.open()
215                 }
216             }
217             MenuItem
218             {
219                 text: "About"
220                 onClicked: aboutdialog.open()
221             }
222
223         }
224     }
225
226     SelectionDialog
227     {
228         id: lyricssrcdialog
229         titleText: "Download source"
230         selectedIndex: 0
231         model: ListModel
232         {
233             ListElement { name: "AZLyrics" }
234             ListElement { name: "Lyric Wiki" }
235             ListElement { name: "Chart Lyrics" }
236             ListElement { name: "LyricsMania" }
237             ListElement { name: "Tekstowo" }
238         }
239         onAccepted:
240         {
241             switch (selectedIndex)
242             {
243             case 0: downloader.changeLyricsDownloader(LyricsDownloaderManager.AZLyricsDownloaderClass)
244             case 1: downloader.changeLyricsDownloader(LyricsDownloaderManager.LyricWikiDownloaderClass)
245             case 2: downloader.changeLyricsDownloader(LyricsDownloaderManager.ChartLyricsDownloaderClass)
246             case 3: downloader.changeLyricsDownloader(LyricsDownloaderManager.LyricsManiaDownloaderClass)
247             case 4: downloader.changeLyricsDownloader(LyricsDownloaderManager.TekstowoDownloaderClass)
248             }
249         }
250     }
251
252     QueryDialog
253     {
254         id: notimplementeddialog
255         acceptButtonText: "OK"
256         message: "Sorry! Not implemented yet!"
257     }
258
259     QueryDialog
260     {
261         id: emptyfieldsdialogmain
262         acceptButtonText: "OK"
263         titleText: "Empty fields"
264         message: "Please enter data to all the fields!"
265     }
266     QueryDialog
267     {
268         id: quitdialog
269         acceptButtonText: "OK"
270         rejectButtonText: "Cancel"
271         message: "Are you sure you want to quit MaeLyrica?"
272         onAccepted: Qt.quit()
273     }
274
275     QueryDialog
276     {
277         id: downloadfaileddialog
278         acceptButtonText: "OK"
279         rejectButtonText: "Try again"
280         titleText: "Download failed"
281         message: "Lyrics download failed.<br />The lyrics may be absent in this provider's database"
282         onRejected:
283         {
284             var ret = downloader.perform()
285             errorDialog(ret)
286         }
287     }
288
289     QueryDialog
290     {
291         id: lyricsnotfounddialog
292         acceptButtonText: "OK"
293         titleText: "Lyrics not found"
294         message: "Lyrics not found. There are not present in the " + lyricssrcdialog.model.get(lyricssrcdialog.selectedIndex).name + " database."
295     }
296
297     QueryDialog
298     {
299         id: noInternetDialog
300         acceptButtonText: "OK"
301         rejectButtonText: "Try again"
302         titleText: "No internet connection."
303         message: "No internet connection was available.<br /> Lyrics were not downloaded"
304         onRejected:
305         {
306             pageStack.pop()
307         }
308         onAccepted: pageStack.pop()
309     }
310
311     Connections
312     {
313         target: downloader
314         onNoInternet: noInternetDialog.open()
315     }
316
317     QueryDialog
318     {
319         id: errorparsingdialog
320         acceptButtonText: "OK"
321         rejectButtonText: "Try again"
322         titleText: "Error while parsing"
323         message: "There was an error while parsing. The lyrics may be missing in this provider's database"
324         onRejected:
325         {
326             var ret = downloader.perform()
327             errorDialog(ret)
328         }
329     }
330
331     QueryDialog
332     {
333         id: loadSavedLyricsDialog
334         acceptButtonText: "Yes"
335         rejectButtonText: "No"
336         titleText: "Lyrics already saved"
337         message: "Lyrics for this track have already been saved. Do you want them to be shown?"
338         onAccepted:
339         {
340             readingSaved = true
341             pageStack.push(goLoader.item)
342         }
343         onRejected:
344         {
345             performDownload()
346         }
347     }
348
349     QueryDialog
350     {
351         id: aboutdialog
352         titleText: "MaeLyrica 1.0"
353         message: "<br /> Copyright (C) 2012 Marcin Mielniczuk <br /><br /> If you want to support my work, please consider <a href='https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WUAUBSU3QQLNL'> donating </a> "
354     }
355
356     function performDownload()
357     {
358         programSettings.addRecentSearch(selecttrack.text)
359         var rv = downloader.perform() // return value from lyricsDownloader::perform()
360         errorDialog(rv)
361     }
362
363     function errorDialog(r)
364     {
365         if ( r == 1)
366         {
367             downloadfaileddialog.open()
368         }
369         else if ( r == 2 )
370         {
371             errorparsingdialog.open()
372         }
373         else if (r == 3)
374         {
375             lyricsnotfounddialog.open()
376         }
377         else
378         {
379             pageStack.push(goLoader.item)
380         }
381     }
382 }
383