1- var fs = require ( "fs" )
1+ var fs = require ( "fs" )
22var parseTorrent = require ( 'parse-torrent' )
3- var Md5 = require ( 'md5' )
3+ var Md5 = require ( 'md5' )
4+ var Defer = require ( "node-promise" ) . defer
45
56var App = require ( 'app' )
67var Ipc = require ( 'ipc' )
@@ -9,6 +10,8 @@ var Dialog = require('dialog')
910var Client = require ( './client' )
1011var LocalStorage = require ( './local-storage' )
1112
13+ var GlobalState = require ( './global-state' )
14+
1215var clients = [ ] ;
1316
1417
@@ -52,10 +55,13 @@ var Manager = function() {
5255 }
5356
5457 function removeClient ( controlHash ) {
55- getItemBy ( clients , 'controlHash' , controlHash ) . teardown ( ) ;
56- removeItemBy ( clients , 'controlHash' , controlHash ) ;
58+ var _client = getItemBy ( clients , 'controlHash' , controlHash ) ;
59+ if ( _client ) {
60+ _client . teardown ( ) ;
61+ removeItemBy ( clients , 'controlHash' , controlHash ) ;
5762
58- LocalStorage . saveClients ( getClients ( ) ) ;
63+ LocalStorage . saveClients ( getClients ( ) ) ;
64+ }
5965 }
6066
6167 function restoreClients ( ) {
@@ -85,31 +91,69 @@ var Manager = function() {
8591 }
8692
8793 function quit ( ) {
88- LocalStorage . saveClients ( getClients ( ) ) ;
94+ var deferred = Defer ( ) ;
95+ LocalStorage . saveClients ( getClients ( ) ) . then ( function ( ) {
96+ deferred . resolve ( ) ;
97+ } ) ;
98+ return deferred . promise ;
8999 }
90100
91- function bindIpc ( ) {
92- Ipc . on ( 'add-client-from-magnet' , function ( event , magnet ) {
93- if ( getItemBy ( clients , 'magnet' , magnet ) ) {
94- Dialog . showMessageBox ( {
95- type : 'error' ,
96- buttons : [ 'ok' ] ,
97- title : 'Duplicate torrent' ,
98- message : 'Duplicate torrent' ,
99- detail : 'This torrent is already exist in the queue.'
100- } , function ( ) { } ) ;
101- return ;
101+ function openUrl ( magnet ) {
102+ if ( getItemBy ( clients , 'magnet' , magnet ) ) {
103+ Dialog . showMessageBox ( {
104+ type : 'error' ,
105+ buttons : [ 'ok' ] ,
106+ title : 'Duplicate torrent' ,
107+ message : 'Duplicate torrent' ,
108+ detail : 'This torrent is already exist in the queue.'
109+ } , function ( ) { } ) ;
110+ return ;
111+ }
112+
113+ Dialog . showOpenDialog ( {
114+ title : 'Select download path' ,
115+ properties : [ 'openDirectory' ]
116+ } , function ( paths ) {
117+ if ( paths ) {
118+ addClient ( magnet , paths [ 0 ] ) ;
119+
120+ var Window = GlobalState . getWindow ( ) ;
121+ if ( Window ) { Window . webContents . send ( 'client-list-refresh' , getClients ( ) ) ; }
102122 }
123+ } ) ;
124+ }
103125
104- Dialog . showOpenDialog ( {
105- title : 'Select download path' ,
106- properties : [ 'openDirectory' ]
107- } , function ( paths ) {
108- if ( paths ) {
109- addClient ( magnet , paths [ 0 ] ) ;
110- event . sender . send ( 'client-list-refresh' , getClients ( ) ) ;
111- }
112- } ) ;
126+ function openFile ( file ) {
127+ var result = parseTorrent ( fs . readFileSync ( file ) ) ,
128+ magnet = parseTorrent . toMagnetURI ( result ) ;
129+
130+ if ( getItemBy ( clients , 'magnet' , magnet ) ) {
131+ Dialog . showMessageBox ( {
132+ type : 'error' ,
133+ buttons : [ 'ok' ] ,
134+ title : 'Duplicate torrent' ,
135+ message : 'Duplicate torrent' ,
136+ detail : 'This torrent is already exist in the queue.'
137+ } , function ( ) { } ) ;
138+ return ;
139+ }
140+
141+ Dialog . showOpenDialog ( {
142+ title : 'Select download path' ,
143+ properties : [ 'openDirectory' ]
144+ } , function ( paths ) {
145+ if ( paths ) {
146+ addClient ( magnet , paths [ 0 ] ) ;
147+
148+ var Window = GlobalState . getWindow ( ) ;
149+ if ( Window ) { Window . webContents . send ( 'client-list-refresh' , getClients ( ) ) ; }
150+ }
151+ } ) ;
152+ }
153+
154+ function bindIpc ( ) {
155+ Ipc . on ( 'add-client-from-magnet' , function ( event , magnet ) {
156+ openUrl ( magnet ) ;
113157 } ) ;
114158
115159 Ipc . on ( 'add-client-from-torrent' , function ( event ) {
@@ -121,29 +165,7 @@ var Manager = function() {
121165 ]
122166 } , function ( torrentPaths ) {
123167 if ( torrentPaths ) {
124- var result = parseTorrent ( fs . readFileSync ( torrentPaths [ 0 ] ) ) ,
125- magnet = parseTorrent . toMagnetURI ( result ) ;
126-
127- if ( getItemBy ( clients , 'magnet' , magnet ) ) {
128- Dialog . showMessageBox ( {
129- type : 'error' ,
130- buttons : [ 'ok' ] ,
131- title : 'Duplicate torrent' ,
132- message : 'Duplicate torrent' ,
133- detail : 'This torrent is already exist in the queue.'
134- } , function ( ) { } ) ;
135- return ;
136- }
137-
138- Dialog . showOpenDialog ( {
139- title : 'Select download path' ,
140- properties : [ 'openDirectory' ]
141- } , function ( paths ) {
142- if ( paths ) {
143- addClient ( magnet , paths [ 0 ] ) ;
144- event . sender . send ( 'client-list-refresh' , getClients ( ) ) ;
145- }
146- } ) ;
168+ openFile ( torrentPaths [ 0 ] ) ;
147169 }
148170 } ) ;
149171 } ) ;
@@ -159,20 +181,21 @@ var Manager = function() {
159181 Ipc . on ( 'client-stop' , function ( event , controlHashes ) {
160182 controlHashes . forEach ( function ( controlHash ) {
161183 var c = getItemBy ( clients , 'controlHash' , controlHash )
162- if ( c . can ( 'stop' ) ) { c . stop ( ) ; }
184+ if ( c && c . can ( 'stop' ) ) { c . stop ( ) ; }
163185 } ) ;
164186 } ) ;
165187
166188 Ipc . on ( 'client-resume' , function ( event , controlHashes ) {
167189 controlHashes . forEach ( function ( controlHash ) {
168190 var c = getItemBy ( clients , 'controlHash' , controlHash )
169- if ( c . can ( 'resume' ) ) { c . resume ( ) ; }
191+ if ( c && c . can ( 'resume' ) ) { c . resume ( ) ; }
170192 } ) ;
171193 } ) ;
172194
173195 Ipc . on ( 'client-open-path' , function ( event , controlHashes ) {
174196 controlHashes . forEach ( function ( controlHash ) {
175- getItemBy ( clients , 'controlHash' , controlHash ) . openPath ( ) ;
197+ var c = getItemBy ( clients , 'controlHash' , controlHash )
198+ if ( c ) { c . openPath ( ) ; }
176199 } ) ;
177200 } ) ;
178201
@@ -184,6 +207,8 @@ var Manager = function() {
184207 return {
185208 bindIpc : bindIpc ,
186209 restore : restore ,
210+ openFile : openFile ,
211+ openUrl : openUrl ,
187212 quit : quit
188213 } ;
189214}
0 commit comments