~unity-team/music-app/infographics-translations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
172
173
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
205
206
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright (C) 2013, 2014, 2015
 *      Andrew Hayzen <ahayzen@gmail.com>
 *      Daniel Holm <d.holmen@gmail.com>
 *      Victor Thompson <victor.thompson@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// First, let's create a short helper function to get the database connection
function getDatabase() {
     return LocalStorage.openDatabaseSync("music-app-metadata", "1.0", "StorageDatabase", 1000000);
}

function createQueue(tx) {
    if (tx === undefined) {
        var db = getDatabase();
        db.transaction(
            function(tx) {
                createQueue(tx)
            }
        )
    } else {
        tx.executeSql("CREATE TABLE IF NOT EXISTS queue(ind INTEGER NOT NULL, filename TEXT)");
    }
}

function clearQueue() {
    var db = getDatabase();
    db.transaction(
        function(tx) {
            createQueue();
            tx.executeSql('DELETE FROM queue');
      });
}

function addQueueItem(filename) {
    var db = getDatabase();
    var res="";

    db.transaction(function(tx) {
        var ind = getNextIndex(tx);

        var rs = tx.executeSql('INSERT OR REPLACE INTO queue (ind, filename) VALUES (?,?);', [ind, filename]);
              if (rs.rowsAffected > 0) {
                console.log("QUEUE add OK")
                res = "OK";
              } else {
                console.log("QUEUE add Fail")
                res = "Error";
              }
        }
    );
}

function addQueueList(items) {
    var db = getDatabase();

    db.transaction(function(tx) {
        var ind = getNextIndex(tx);

        for (var i = 0; i < items.length; i++) {
            tx.executeSql('INSERT OR REPLACE INTO queue (ind, filename) VALUES (?,?);', [i + ind, items[i].filename]);
        }
    }
    );
}

// Get the next index for the queue
function getNextIndex(tx) {
    var ind;

    if (tx === undefined) {
        var db = getDatabase();

        db.transaction(function(tx) {
            ind = getNextIndex(tx);
        });
    } else {
        var rs = tx.executeSql('SELECT MAX(ind) FROM queue')
        ind = isQueueEmpty(tx) ? 0 : rs.rows.item(0)["MAX(ind)"] + 1
    }

    return ind;
}

function moveQueueItem(from, to) {
    var db = getDatabase();
    var res="";

    db.transaction(function(tx) {
        // Track to move put as -1 for now
        tx.executeSql('UPDATE queue SET ind=? WHERE ind=?;',
                      [-1, from])

        // Shuffle tracks inbetween from->to
        if (from > to) {
            for (var i = from-1; i >= to; i--) {
                tx.executeSql('UPDATE queue SET ind=? WHERE ind=?;',
                              [i+1, i])
            }
        } else {
            for (var j = from+1; j <= to; j++) {
                tx.executeSql('UPDATE queue SET ind=? WHERE ind=?;',
                              [j-1, j])
            }
        }

        // Switch moving track to its new position
        tx.executeSql('UPDATE queue SET ind=? WHERE ind=?;',
                      [to, -1])

    })
}


// Optimised removeQueue for removing multiple tracks from the queue
function removeQueueList(list)
{
    var db = getDatabase()
    var i;
    var res = false

    db.transaction(function (tx) {
        // Remove all the deleted indexes
        for (i=0; i < list.length; i++) {
            tx.executeSql('DELETE FROM queue WHERE ind=?;', [list[i]])
        }

        // Rebuild queue in order
        var rs = tx.executeSql('SELECT ind FROM queue ORDER BY ind ASC')

        for (i=0; i < rs.rows.length; i++) {
            tx.executeSql('UPDATE queue SET ind=? WHERE ind=?;',
                          [i, rs.rows.item(i).ind])
        }
    })

    return res
}


function getQueue() {
    var res = [];
    var db = getDatabase();
    db.transaction( function(tx) {
        var rs = tx.executeSql("SELECT * FROM queue ORDER BY ind ASC");
        for(var i = 0; i < rs.rows.length; i++) {
            if (musicStore.lookup(rs.rows.item(i).filename) != null) {
                res.push(makeDict(musicStore.lookup(rs.rows.item(i).filename)));
            }
        }
    });
    return res;
}

function isQueueEmpty(tx) {
    var empty = false;

    if (tx === undefined) {
        var db = getDatabase();
        var res = 0;

        db.transaction( function(tx) {
            empty = isQueueEmpty(tx)
        });
    } else {
        createQueue(tx);
        var rs = tx.executeSql("SELECT count(*) as value FROM queue")
        empty = rs.rows.item(0).value === 0
    }

    return empty
}

function createRecent() {
    var db = getDatabase();
    db.transaction(
        function(tx) {
            // Check of old version of db (or no db) then clear and rebuild if needed
            try {
                tx.executeSql("SELECT data FROM recent");
            } catch (e) {
                tx.executeSql('DROP TABLE IF EXISTS recent');

                // Data is either the playlist name or album name
                tx.executeSql("CREATE TABLE IF NOT EXISTS recent(time DATETIME UNIQUE, data TEXT, type TEXT)");
            }
      });
}

function clearRecentHistory() {
    var db = getDatabase();
    db.transaction(
        function(tx) {
            tx.executeSql('DELETE FROM recent');
      });
}


// This function is used to insert a recent item into the database
function addRecent(data, type) {
    var db = getDatabase();

    console.debug("RECENT", data, type);


    db.transaction(function (tx) {
        // Remove old albums/playlists with same name as they have a new time
        if (type === "album") {
            tx.executeSql("DELETE FROM recent WHERE type=? AND data=?", ["album", data])
        } else if (type === "playlist") {
            tx.executeSql("DELETE FROM recent WHERE type=? AND data=?", ["playlist", data])
        }

        var rs = tx.executeSql('INSERT OR REPLACE INTO recent (time, data, type) VALUES (?, ?, ?)', [new Date(), data, type]);

        if (rs.rowsAffected <= 0) {
            console.debug("RECENT add Fail")
        }
    });
}

function getRecent() {
    var res = [];
    var db = getDatabase();

    db.transaction( function(tx) {
        var rs = tx.executeSql("SELECT * FROM recent ORDER BY time DESC LIMIT 15");
        for(var i = 0; i < rs.rows.length; i++) {
            var dbItem = rs.rows.item(i);

            console.log("Time:", dbItem.time, ", Data:", dbItem.data, ", Type:", dbItem.type);

            res.push({"time": dbItem.time, "data": dbItem.data, "type": dbItem.type});
        }
    });
    return res;
}

function recentContainsPlaylist(key) {
    var db = getDatabase();
    var rs;

    db.transaction(function(tx) {
        rs = tx.executeSql("SELECT count(*) as value FROM recent WHERE type=? AND data=?",
                           ["playlist", key]);
    });

    return rs.rows.item(0).value > 0;
}

// Remove albums from recent by album
function recentRemoveAlbums(albums)
{
    var db = getDatabase();

    db.transaction( function(tx) {
        for (var i=0; i < albums.length; i++) {
            tx.executeSql("DELETE FROM recent WHERE type=? AND data=?",
                          ["album", albums[i]]);
        }
    })
}

function recentRemovePlaylist(key) {
    var res = false
    var db = getDatabase();

    db.transaction( function(tx) {
        res = tx.executeSql("DELETE FROM recent WHERE type=? AND data=?",
                            ["playlist", key]).rowsAffected > 0;

    })

    return res;
}

function recentRenamePlaylist(oldKey, newKey) {
    var db = getDatabase();

    db.transaction( function(tx) {
        tx.executeSql("UPDATE recent SET data=? WHERE type=? AND data=?",
                      [newKey, "playlist", oldKey]);

    });
}

function isRecentEmpty() {
    var db = getDatabase();
    var res = 0;

    db.transaction(function(tx) {
        var rs;

        try {
            rs = tx.executeSql("SELECT count(*) as value FROM recent")
        } catch (e) {
            rs = null
        }

        if (rs !== null && rs.rows.item(0).value > 0) {
            res = rs.rows.item(0).value;
        } else {
            console.log("RECENT does not exist")
            res = 0;
        }
    });

    return res === 0;
}