summaryrefslogtreecommitdiffstats
path: root/apps/media/js/scanner.js
blob: 165f86d05f5177c053df3c618649e64963ddf4e7 (plain)
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
Scanner={
	songsFound:0,
	songsScanned:0,
	songsChecked:0,
	startTime:null,
	endTime:null,
	stopScanning:false,
	currentIndex:-1,
	songs:[],
	findSongs:function(ready){
		$.getJSON(OC.linkTo('media','ajax/api.php')+'?action=find_music',function(songs){
			Scanner.songsFound=songs.length;
			Scanner.currentIndex=-1
			if(ready){
				ready(songs)
			}
		});
	},
	scanFile:function(path,ready){
		path=encodeURIComponent(path);
		$.getJSON(OC.linkTo('media','ajax/api.php')+'?action=get_path_info&path='+path,function(song){
			Scanner.songsChecked++;
			if(ready){
				ready(song);
			}
			if(song){//do this after the ready call so we dont hold up the next ajax call
				var artistId=song.song_artist;
				Scanner.songsScanned++;
				$('#scan span.songCount').text(Scanner.songsScanned);
				var progress=(Scanner.songsChecked/Scanner.songsFound)*100;
				$('#scanprogressbar').progressbar('value',progress)
				Collection.addSong(song);
			}
		});
	},
	scanCollection:function(ready){
		$('#scanprogressbar').progressbar({
			value:0,
		});
		Scanner.songsChecked=0;
		Scanner.songsScanned=0;
		Scanner.startTime=new Date().getTime()/1000;
		Scanner.findSongs(function(songs){
			Scanner.songs=songs;
			Scanner.start();
		});
	},
	stop:function(){
		Scanner.stopScanning=true;
	},
	start:function(ready){
		Scanner.stopScanning=false;
		$('#scancount').show();
		var scanSong=function(){
			Scanner.currentIndex++;
			if(!Scanner.stopScanning && Scanner.currentIndex<Scanner.songs.length){
				Scanner.scanFile(Scanner.songs[Scanner.currentIndex],scanSong)
			}else{
				Scanner.endTime=new Date().getTime()/1000;
				if(ready){
					ready();
				}
			}
		}
		scanSong();
		scanSong();
	},
	toggle:function(){
		if(Scanner.stopScanning){
			Scanner.start();
			$('#scan input.stop').val('Pause');
		}else{
			Scanner.stop();
			$('#scan input.stop').val('Resume');
		}
	}

}