dialog-auto-match-data-folder.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <div class="easyui-layout" data-options="fit:true" style="width:100%;height:100%;">
  2. <div data-options="region:'center'" style="padding:3px;border:0px;">
  3. <div id="" class="dialog" style="width:100%;padding:0px;">
  4. <table style="width:100%;">
  5. <tr>
  6. <td _width="30%" class="title"><span id="dialog-auto-match-data-folder-torrent-count" system-lang="dialog['auto-match-data-folder']['torrent-count']"></span></td>
  7. <td _width="50%">
  8. <span id="torrent-count"></span>
  9. </td>
  10. <td _width="20%"></td>
  11. </tr>
  12. <tr>
  13. <td class="title"><span id="dialog-auto-match-data-folder-folder-count" system-lang="dialog['auto-match-data-folder']['folder-count']"></span></td>
  14. <td>
  15. <span id="folder-count"></span>
  16. </td>
  17. <td>
  18. <a id="button-config" class="easyui-linkbutton" data-options="iconCls:'icon-system-config',plain:true" href="javascript:void(0);"><span system-lang="dialog['public']['button-config']"></span></a>
  19. </td>
  20. </tr>
  21. <tr style="display:none;">
  22. <td class="title"><span id="dialog-auto-match-data-folder-time-begin" system-lang="dialog['auto-match-data-folder']['time-begin']"></span></td>
  23. <td>
  24. <span id="time-begin"></span>
  25. </td>
  26. <td></td>
  27. </tr>
  28. <tr style="display:none;">
  29. <td class="title"><span id="dialog-auto-match-data-folder-time-now" system-lang="dialog['auto-match-data-folder']['time-now']"></span></td>
  30. <td>
  31. <span id="time-now"></span>
  32. </td>
  33. <td></td>
  34. </tr>
  35. <tr>
  36. <td class="title">
  37. <span system-lang="dialog['auto-match-data-folder']['work-mode-title']"></span>
  38. </td>
  39. <td>
  40. <select id="work-mode" class="easyui-combobox" data-options="editable:false" style="width:200px;"></select>
  41. </td>
  42. <td></td>
  43. </tr>
  44. <tr>
  45. <td class="title">
  46. <span system-lang="dialog['auto-match-data-folder']['time-interval']"></span>
  47. </td>
  48. <td>
  49. <input id="time-interval" value="3" type="text" class="easyui-numberspinner" style="width:200px;" data-options="min:0.5,precision:1,increment:0.5"/>
  50. </td>
  51. <td></td>
  52. </tr>
  53. <tr>
  54. <td class="title" colspan="3" style="text-align:left;"><span id="dialog-auto-match-data-folder-status" system-lang="dialog['auto-match-data-folder']['status']"></span></td>
  55. </tr>
  56. <tr>
  57. <td colspan="3" id="td-status" style="display:none;">
  58. <span id="cus-torrent"></span><a id="button-ignore" class="easyui-linkbutton" data-options="iconCls:'icon-ignore',plain:true" href="javascript:void(0);"><span system-lang="dialog['auto-match-data-folder']['ignore']"></span></a><br/>
  59. <span id="cus-folder"></span>
  60. </td>
  61. </tr>
  62. </table>
  63. </div>
  64. </div>
  65. <div data-options="region:'south',border:false" style="text-align:right;padding:6px;">
  66. <span id="text-nochange" style="display:none;"></span>
  67. <span id="text-count"></span>
  68. <a id="button-ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" href="javascript:void(0);">Ok</a>
  69. <a id="button-cancel" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" href="javascript:void(0);">Cancel</a>
  70. </div>
  71. </div>
  72. <script type="text/javascript">
  73. (function(thisDialog){
  74. var ids = thisDialog.data("ids");
  75. var notMatchedTorrents = new Array();
  76. var notMatchedFolders = new Array();
  77. var torrent = null;
  78. var isworking = false;
  79. var ignore = false;
  80. var timeInterval = parseInt(system.getStorageData("auto-match-data-folder-time-interval",3000));
  81. if (typeof(ids)=="object")
  82. {
  83. torrent = transmission.torrents.all[ids[0]];
  84. thisDialog.find("#torrent-count").html(ids.length);
  85. }
  86. else
  87. {
  88. torrent = transmission.torrents.all[ids];
  89. thisDialog.find("#torrent-count").html(1);
  90. ids = null;
  91. ids = new Array();
  92. ids.push(torrent.id);
  93. }
  94. if (!torrent)
  95. {
  96. thisDialog.dialog("close");
  97. return;
  98. }
  99. $.each(ids,function(key,value){
  100. torrent = transmission.torrents.all[value];
  101. if (torrent.status==0&&torrent.percentDone==0)
  102. {
  103. notMatchedTorrents.push(value);
  104. }
  105. });
  106. system.resetLangText(thisDialog);
  107. var mode = system.lang.dialog["auto-match-data-folder"]["work-mode"];
  108. for (var key in mode)
  109. {
  110. $("<option/>").val(key).attr("selected",(key==system.getStorageData("auto-match-data-folder-work-mode",1)?true:false)).text(mode[key]).appendTo(thisDialog.find("#work-mode"));
  111. }
  112. thisDialog.find("#time-interval").val(timeInterval/1000);
  113. $.each("button-ok,button-cancel".split(","), function(i, item){
  114. thisDialog.find("#"+item).html(system.lang.dialog["public"][item]);
  115. });
  116. // Cancel
  117. thisDialog.find("#button-cancel").click(function()
  118. {
  119. if (isworking)
  120. {
  121. if (confirm(system.lang.dialog["auto-match-data-folder"]["working-close-confirm"]))
  122. {
  123. isworking = false;
  124. thisDialog.dialog("close");
  125. return;
  126. }
  127. }
  128. else
  129. {
  130. thisDialog.dialog("close");
  131. }
  132. });
  133. // Ignore
  134. thisDialog.find("#button-ignore").click(function()
  135. {
  136. if (isworking&&!ignore)
  137. {
  138. ignore = true;
  139. }
  140. });
  141. // Set up
  142. thisDialog.find("#button-config").click(function()
  143. {
  144. system.openDialogFromTemplate({
  145. id: "dialog-auto-match-data-folder-dictionary",
  146. options: {
  147. title: system.lang.dialog["auto-match-data-folder"]["dictionary"],
  148. width: 530,
  149. height: 280
  150. },
  151. datas: {
  152. callback: function(){
  153. notMatchedFolders = system.dictionary.folders.split("\n");
  154. thisDialog.find("#folder-count").html(notMatchedFolders.length);
  155. }
  156. }
  157. });
  158. });
  159. if (system.dictionary.folders==null)
  160. {
  161. thisDialog.find("#button-ok").prop("disabled",true);
  162. thisDialog.find("#folder-count").html(0);
  163. return;
  164. }
  165. var notMatchedFolders = system.dictionary.folders.split("\n");
  166. var torrentIndex = 0;
  167. var folderIndex = 0;
  168. thisDialog.find("#folder-count").html(notMatchedFolders.length);
  169. // Confirm
  170. thisDialog.find("#button-ok").click(function()
  171. {
  172. system.control.torrentlist.datagrid("uncheckAll");
  173. var button = $(this);
  174. var icon = button.linkbutton("options").iconCls;
  175. button.linkbutton({disabled:true,iconCls:"icon-loading"});
  176. isworking = true;
  177. thisDialog.find("#td-status").show();
  178. var modeType = thisDialog.find("#work-mode").combobox("getValue");
  179. timeInterval = thisDialog.find("#time-interval").val()*1000;
  180. switch (parseInt(modeType))
  181. {
  182. // method one
  183. case 1:
  184. thisDialog.find("#button-ignore").show();
  185. thisDialog.find("#cus-torrent").html("-> "+torrent.name + " [" + (torrentIndex+1)+"/"+notMatchedTorrents.length + "] ");
  186. var _arrSource = $.map(notMatchedFolders, function(n){return n;});
  187. execMatchDataFolderMode1(_arrSource,_arrSource.length,button,icon,notMatchedTorrents[torrentIndex]);
  188. _arrSource = null;
  189. break;
  190. // method two
  191. case 2:
  192. thisDialog.find("#button-ignore").hide();
  193. thisDialog.find("#cus-folder").html("-> "+notMatchedFolders[folderIndex] + " [" + (folderIndex+1)+"/"+notMatchedFolders.length + "] ");
  194. var _arrSource = $.map(notMatchedTorrents, function(n){return n;});
  195. execMatchDataFolderMode3(_arrSource,_arrSource.length,button,icon,notMatchedFolders[folderIndex]);
  196. _arrSource = null;
  197. break;
  198. }
  199. system.setStorageData("auto-match-data-folder-work-mode",modeType);
  200. system.setStorageData("auto-match-data-folder-time-interval",timeInterval);
  201. });
  202. // method one
  203. function execMatchDataFolderMode1(folders,count,button,icon,id)
  204. {
  205. if (isworking==false) return;
  206. //alert("count:"+count+",ids.length:"+ids.length+",notMatchedTorrents.length:"+notMatchedTorrents.length);
  207. //var id = ids;
  208. var index = count-folders.length;
  209. var dir = folders.shift();
  210. var torrent = transmission.torrents.all[id];
  211. if (!dir||ignore||torrent.percentDone!=0)
  212. {
  213. ignore = false;
  214. torrentIndex++;
  215. if (torrentIndex<notMatchedTorrents.length&&isworking)
  216. {
  217. system.debug("torrent:",torrent.name);
  218. thisDialog.find("#cus-torrent").html("-> "+torrent.name + " [" + (torrentIndex+1)+"/"+notMatchedTorrents.length + "] ");
  219. var _arrSource = $.map(notMatchedFolders, function(n){return n;});
  220. execMatchDataFolderMode1(_arrSource,_arrSource.length,button,icon,notMatchedTorrents[torrentIndex]);
  221. _arrSource = null;
  222. }
  223. else
  224. {
  225. button.linkbutton({iconCls:icon,disabled:false});
  226. thisDialog.dialog("close");
  227. }
  228. return;
  229. }
  230. thisDialog.find("#cus-folder").html("-> "+dir + " [" + (index+1)+"/"+count + "] ");
  231. if (torrent.downloadDir==dir)
  232. {
  233. execMatchDataFolderMode1(folders,count,button,icon,id);
  234. return;
  235. }
  236. // Start setting parameters
  237. transmission.exec(
  238. {
  239. method:"torrent-set-location"
  240. ,arguments:{
  241. ids:id
  242. ,location:dir
  243. ,move:false
  244. }
  245. }
  246. ,function(data){
  247. if (data.result=="success")
  248. {
  249. torrent.downloadDir = dir;
  250. transmission.exec(
  251. {
  252. method:"torrent-verify"
  253. ,arguments:{
  254. ids:id
  255. }
  256. }
  257. ,function(data1){
  258. setTimeout(function(){
  259. transmission.exec(
  260. {
  261. method:"torrent-get"
  262. ,arguments:{
  263. ids:id
  264. ,fields: ["percentDone"]
  265. }
  266. }
  267. ,function(data2){
  268. if (data2.result=="success")
  269. {
  270. //
  271. if (data2.arguments.torrents[0].percentDone!=0)
  272. {
  273. torrentIndex++;
  274. if (torrentIndex<notMatchedTorrents.length&&isworking)
  275. {
  276. system.debug("torrent:",torrent.name);
  277. thisDialog.find("#cus-torrent").html("-> "+torrent.name + " [" + (torrentIndex+1)+"/"+notMatchedTorrents.length + "] ");
  278. var _arrSource = $.map(notMatchedFolders, function(n){return n;});
  279. execMatchDataFolderMode1(_arrSource,_arrSource.length,button,icon,notMatchedTorrents[torrentIndex]);
  280. _arrSource = null;
  281. }
  282. else
  283. {
  284. button.linkbutton({iconCls:icon,disabled:false});
  285. thisDialog.dialog("close");
  286. }
  287. }
  288. else
  289. {
  290. execMatchDataFolderMode1(folders,count,button,icon,id);
  291. }
  292. }
  293. }
  294. );
  295. },timeInterval)
  296. }
  297. );
  298. }
  299. else
  300. {
  301. button.linkbutton({iconCls:icon,disabled:false});
  302. $.messager.alert("",system.lang.error["data-post-error"]+": "+data.result,'error');
  303. }
  304. }
  305. );
  306. }
  307. // method two
  308. function execMatchDataFolderMode2(ids,count,button,icon,dir)
  309. {
  310. if (isworking==false) return;
  311. //alert("count:"+count+",ids.length:"+ids.length+",notMatchedTorrents.length:"+notMatchedTorrents.length);
  312. //var id = ids;
  313. var index = count-ids.length;
  314. var id = ids.shift();
  315. if (!id)
  316. {
  317. folderIndex++;
  318. if (folderIndex<notMatchedFolders.length&&isworking)
  319. {
  320. setTimeout(function(){
  321. transmission.exec(
  322. {
  323. method:"torrent-get"
  324. ,arguments:{
  325. ids:notMatchedTorrents
  326. ,fields: ["status","id","percentDone"]
  327. }
  328. }
  329. ,function(data){
  330. if (data.result=="success")
  331. {
  332. notMatchedTorrents = null;
  333. notMatchedTorrents = new Array();
  334. $.each(data.arguments.torrents,function(key,item){
  335. if (item.status==0&&item.percentDone==0)
  336. {
  337. if (jQuery.inArray(item.id,notMatchedTorrents)==-1)
  338. notMatchedTorrents.push(item.id);
  339. }
  340. });
  341. if (notMatchedTorrents.length>0)
  342. {
  343. thisDialog.find("#cus-folder").html("-> "+notMatchedFolders[folderIndex] + " [" + (folderIndex+1)+"/"+notMatchedFolders.length + "] ");
  344. system.debug("folder:",notMatchedFolders[folderIndex]);
  345. var _arrSource = $.map(notMatchedTorrents, function(n){return n;});
  346. execMatchDataFolderMode2(_arrSource,_arrSource.length,button,icon,notMatchedFolders[folderIndex]);
  347. _arrSource = null;
  348. }
  349. else
  350. {
  351. system.control.torrentlist.datagrid("uncheckAll");
  352. button.linkbutton({iconCls:icon,disabled:false});
  353. thisDialog.dialog("close");
  354. }
  355. }
  356. else
  357. {
  358. system.control.torrentlist.datagrid("uncheckAll");
  359. button.linkbutton({iconCls:icon,disabled:false});
  360. thisDialog.dialog("close");
  361. }
  362. }
  363. );
  364. },timeInterval);
  365. }
  366. else
  367. {
  368. system.control.torrentlist.datagrid("uncheckAll");
  369. button.linkbutton({iconCls:icon,disabled:false});
  370. thisDialog.dialog("close");
  371. }
  372. return;
  373. }
  374. var torrent = transmission.torrents.all[id];
  375. system.debug("torrent:",torrent.name);
  376. thisDialog.find("#cus-torrent").html("-> "+torrent.name + " [" + (index+1)+"/"+count + "] ");
  377. if (torrent.downloadDir==dir)
  378. {
  379. if (torrent.percentDone==0&&torrent.status==0)
  380. {
  381. if (jQuery.inArray(id,notMatchedTorrents)==-1)
  382. notMatchedTorrents.push(id);
  383. }
  384. execMatchDataFolderMode2(ids,count,button,icon,dir);
  385. return;
  386. }
  387. else if (torrent.percentDone!=0)
  388. {
  389. execMatchDataFolderMode2(ids,count,button,icon,dir);
  390. return;
  391. }
  392. // Start setting parameters
  393. transmission.exec(
  394. {
  395. method:"torrent-set-location"
  396. ,arguments:{
  397. ids:id
  398. ,location:dir
  399. ,move:false
  400. }
  401. }
  402. ,function(data){
  403. if (data.result=="success")
  404. {
  405. torrent.downloadDir = dir;
  406. transmission.exec(
  407. {
  408. method:"torrent-verify"
  409. ,arguments:{
  410. ids:id
  411. }
  412. }
  413. ,function(data){
  414. execMatchDataFolderMode2(ids,count,button,icon,dir);
  415. }
  416. );
  417. }
  418. else
  419. {
  420. button.linkbutton({iconCls:icon,disabled:false});
  421. $.messager.alert("",system.lang.error["data-post-error"]+": "+data.result,'error');
  422. }
  423. }
  424. );
  425. }
  426. // method two
  427. function execMatchDataFolderMode3(ids,count,button,icon,dir)
  428. {
  429. if (isworking==false) return;
  430. thisDialog.find("#cus-torrent").html("-> "+count);
  431. // Start setting parameters
  432. transmission.exec(
  433. {
  434. method:"torrent-set-location"
  435. ,arguments:{
  436. ids:ids
  437. ,location:dir
  438. ,move:false
  439. }
  440. }
  441. ,function(data){
  442. if (data.result=="success")
  443. {
  444. torrent.downloadDir = dir;
  445. transmission.exec(
  446. {
  447. method:"torrent-verify"
  448. ,arguments:{
  449. ids:ids
  450. }
  451. }
  452. ,function(data){
  453. folderIndex++;
  454. if (folderIndex<notMatchedFolders.length&&isworking)
  455. {
  456. setTimeout(function(){
  457. transmission.exec(
  458. {
  459. method:"torrent-get"
  460. ,arguments:{
  461. ids:ids
  462. ,fields: ["status","id","percentDone"]
  463. }
  464. }
  465. ,function(data){
  466. if (data.result=="success")
  467. {
  468. notMatchedTorrents = null;
  469. notMatchedTorrents = new Array();
  470. $.each(data.arguments.torrents,function(key,item){
  471. if (item.status==0&&item.percentDone==0)
  472. {
  473. if (jQuery.inArray(item.id,notMatchedTorrents)==-1)
  474. notMatchedTorrents.push(item.id);
  475. }
  476. });
  477. if (notMatchedTorrents.length>0)
  478. {
  479. thisDialog.find("#cus-folder").html("-> "+notMatchedFolders[folderIndex] + " [" + (folderIndex+1)+"/"+notMatchedFolders.length + "] ");
  480. system.debug("folder:",notMatchedFolders[folderIndex]);
  481. var _arrSource = $.map(notMatchedTorrents, function(n){return n;});
  482. execMatchDataFolderMode3(_arrSource,_arrSource.length,button,icon,notMatchedFolders[folderIndex]);
  483. _arrSource = null;
  484. }
  485. else
  486. {
  487. system.control.torrentlist.datagrid("uncheckAll");
  488. button.linkbutton({iconCls:icon,disabled:false});
  489. thisDialog.dialog("close");
  490. }
  491. }
  492. else
  493. {
  494. system.control.torrentlist.datagrid("uncheckAll");
  495. button.linkbutton({iconCls:icon,disabled:false});
  496. thisDialog.dialog("close");
  497. }
  498. }
  499. );
  500. },timeInterval);
  501. }
  502. else
  503. {
  504. system.control.torrentlist.datagrid("uncheckAll");
  505. button.linkbutton({iconCls:icon,disabled:false});
  506. thisDialog.dialog("close");
  507. }
  508. }
  509. );
  510. }
  511. else
  512. {
  513. button.linkbutton({iconCls:icon,disabled:false});
  514. $.messager.alert("",system.lang.error["data-post-error"]+": "+data.result,'error');
  515. }
  516. }
  517. );
  518. }
  519. })($("#dialog-auto-match-data-folder"));
  520. </script>