OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This Source Code is subject to the terms of the Mozilla Public License |
| 3 * version 2.0 (the "License"). You can obtain a copy of the License at |
| 4 * http://mozilla.org/MPL/2.0/. |
| 5 */ |
| 6 |
| 7 /* |
| 8 * crawler_ui.js |
| 9 */ |
| 10 /** |
| 11 * @fileOverview These functions implement the user interface behaviors of the t
op-level control dialog. |
| 12 */ |
| 13 |
| 14 const Cu = Components.utils; |
| 15 const Cc = Components.classes; |
| 16 const Ci = Components.interfaces; |
| 17 |
| 18 Cu.import( "resource://gre/modules/Services.jsm" ); |
| 19 Cu.import( "resource://gre/modules/FileUtils.jsm" ); |
| 20 |
| 21 function require( module ) |
| 22 { |
| 23 let result = {}; |
| 24 result.wrappedJSObject = result; |
| 25 Services.obs.notifyObservers( result, "abpcrawler-require", module ); |
| 26 if ( !("exports" in result) ) |
| 27 { |
| 28 Cu.reportError( "crawler_ui require: 'exports' missing from module \"" + mod
ule + "\"" ); |
| 29 } |
| 30 return result.exports; |
| 31 } |
| 32 let { Storage } = require( "storage" ); |
| 33 let { Logger } = require( "logger" ); |
| 34 let { Application_Session } = require( "application" ); |
| 35 |
| 36 //------------------------------------------------------- |
| 37 // Globals and Handlers |
| 38 //------------------------------------------------------- |
| 39 |
| 40 var crawler_ui_log = (new Logger( "crawler_ui" )).make_log(); |
| 41 |
| 42 var current_session = null; |
| 43 var preference_service, preference_branch; |
| 44 var go_button; |
| 45 var input_file, input_file_initial_value; |
| 46 var output_directory, output_directory_initial_value; |
| 47 var base_name, base_name_initial_value; |
| 48 var number_of_tabs; |
| 49 var time_limit_units; |
| 50 var log_window, progress_message; |
| 51 |
| 52 function loader() |
| 53 { |
| 54 crawler_ui_log( "Loading." ); |
| 55 |
| 56 log_window = new Crawl_Display(); |
| 57 progress_message = document.getElementById( "progress" ); |
| 58 go_button = document.getElementById( "crawl_go" ); |
| 59 preference_service = Cc["@mozilla.org/preferences-service;1"].getService( Ci.n
sIPrefService ); |
| 60 preference_branch = preference_service.getBranch( "extensions.abpcrawler." ); |
| 61 |
| 62 /* |
| 63 * Set up the output directory values and preferences. |
| 64 */ |
| 65 input_file = document.getElementById( "input_file" ); |
| 66 base_name = document.getElementById( "base_name" ); |
| 67 output_directory = document.getElementById( "output_directory" ); |
| 68 |
| 69 if ( preference_branch.prefHasUserValue( "input_file" ) ) |
| 70 { |
| 71 input_file_initial_value = preference_branch.getCharPref( "input_file" ); |
| 72 input_file.value = input_file_initial_value; |
| 73 } |
| 74 base_name_initial_value = base_name.value; |
| 75 if ( preference_branch.prefHasUserValue( "base_name" ) ) |
| 76 { |
| 77 base_name_initial_value = preference_branch.getCharPref( "base_name" ); |
| 78 base_name.value = base_name_initial_value; |
| 79 } |
| 80 else |
| 81 { |
| 82 base_name_initial_value = base_name.value; |
| 83 } |
| 84 if ( preference_branch.prefHasUserValue( "output_directory" ) ) |
| 85 { |
| 86 output_directory_initial_value = preference_branch.getCharPref( "output_dire
ctory" ); |
| 87 output_directory.value = output_directory_initial_value; |
| 88 } |
| 89 else |
| 90 { |
| 91 output_directory_initial_value = ""; |
| 92 var dir = FileUtils.getDir( "Home", [] ); |
| 93 output_directory.value = dir.path; |
| 94 } |
| 95 document.getElementById( "input_file_icon" ).addEventListener( "click", icon_i
nput_click ); |
| 96 document.getElementById( "output_directory_icon" ).addEventListener( "click",
icon_output_click ); |
| 97 |
| 98 time_limit_units = document.getElementById( "time_limit_units" ); |
| 99 time_limit_units.selectedIndex = 1; |
| 100 } |
| 101 |
| 102 function unloader() |
| 103 { |
| 104 crawler_ui_log( "Unloading." ); |
| 105 if ( current_session ) |
| 106 { |
| 107 current_session.close(); |
| 108 current_session = null; |
| 109 } |
| 110 } |
| 111 |
| 112 function icon_input_click() |
| 113 { |
| 114 var fp = Cc["@mozilla.org/filepicker;1"].createInstance( Ci.nsIFilePicker ); |
| 115 fp.init( window, "Select an Input File", Ci.nsIFilePicker.modeOpen ); |
| 116 if ( input_file.value != "" && input_file.value != null ) |
| 117 { |
| 118 var f = new FileUtils.File( input_file.value ); |
| 119 if ( f.exists() ) |
| 120 { |
| 121 if ( f.isFile() ) |
| 122 { |
| 123 f = f.parent; |
| 124 } |
| 125 if ( f.isDirectory() ) |
| 126 { |
| 127 fp.displayDirectory = f; |
| 128 } |
| 129 } |
| 130 } |
| 131 var result = fp.show(); |
| 132 switch ( result ) |
| 133 { |
| 134 case Ci.nsIFilePicker.returnOK: |
| 135 f = fp.file; |
| 136 if ( f.isFile() ) |
| 137 { |
| 138 input_file.value = fp.file.path; |
| 139 } |
| 140 break; |
| 141 case Ci.nsIFilePicker.returnCancel: |
| 142 break; |
| 143 case Ci.nsIFilePicker.returnReplace: |
| 144 break; |
| 145 default: |
| 146 break; |
| 147 } |
| 148 } |
| 149 |
| 150 function icon_output_click() |
| 151 { |
| 152 var fp = Cc["@mozilla.org/filepicker;1"].createInstance( Ci.nsIFilePicker ); |
| 153 fp.init( window, "Select an Output Folder", Ci.nsIFilePicker.modeGetFolder ); |
| 154 var result = fp.show(); |
| 155 switch ( result ) |
| 156 { |
| 157 case Ci.nsIFilePicker.returnOK: |
| 158 output_directory.value = fp.file.path; |
| 159 break; |
| 160 case Ci.nsIFilePicker.returnCancel: |
| 161 break; |
| 162 case Ci.nsIFilePicker.returnReplace: |
| 163 break; |
| 164 default: |
| 165 break; |
| 166 } |
| 167 } |
| 168 |
| 169 //------------------------------------------------------- |
| 170 // Start Crawl |
| 171 //------------------------------------------------------- |
| 172 function start_crawl() |
| 173 { |
| 174 crawler_ui_log( "Start" ); |
| 175 |
| 176 /* |
| 177 * Save preferences automatically when we start a crawl. |
| 178 */ |
| 179 var saving_input = ( input_file_initial_value != input_file.value ); |
| 180 var saving_basename = ( base_name_initial_value != base_name.value ); |
| 181 var saving_dir = ( output_directory.value != output_directory_initial_value ); |
| 182 if ( saving_input ) |
| 183 { |
| 184 preference_branch.setCharPref( "input_file", input_file.value ); |
| 185 } |
| 186 if ( saving_basename ) |
| 187 { |
| 188 preference_branch.setCharPref( "base_name", base_name.value ); |
| 189 } |
| 190 if ( saving_dir ) |
| 191 { |
| 192 preference_branch.setCharPref( "output_directory", output_directory.value ); |
| 193 } |
| 194 if ( saving_input || saving_basename || saving_dir ) |
| 195 { |
| 196 preference_service.savePrefFile( null ); |
| 197 /* |
| 198 * Recalculate initial values only when saving. |
| 199 */ |
| 200 input_file_initial_value = input_file.value; |
| 201 base_name_initial_value = base_name.value; |
| 202 output_directory_initial_value = output_directory.value; |
| 203 } |
| 204 var log_to_textbox = new Storage.Display_Log( log_window ); |
| 205 |
| 206 /* |
| 207 * Encoding |
| 208 */ |
| 209 var encoding = null; |
| 210 switch ( document.getElementById( "format" ).selectedIndex ) |
| 211 { |
| 212 case 0: |
| 213 encoding = "JSON"; |
| 214 break; |
| 215 case 1: |
| 216 encoding = "YAML"; |
| 217 break; |
| 218 default: |
| 219 log_window.log( "Unknown output encoding. Aborted." ); |
| 220 return false; |
| 221 } |
| 222 |
| 223 /* |
| 224 * Window |
| 225 */ |
| 226 let mainWindow = window.opener; |
| 227 if ( !mainWindow || mainWindow.closed ) |
| 228 { |
| 229 log_window.log( "Unable to find the main window, aborting." ); |
| 230 return false; |
| 231 } |
| 232 |
| 233 /* |
| 234 * Miscellaneous |
| 235 */ |
| 236 // Initialize fixed part of the progress message |
| 237 document.getElementById( "progress_label" ).value = "Active/Completed/Total"; |
| 238 |
| 239 /* |
| 240 * Session. Note that we create the session object before the outputs, since t
he session class has multiple ways |
| 241 * of specifying them. |
| 242 */ |
| 243 current_session = new Application_Session( |
| 244 mainWindow, |
| 245 document.getElementById( "leave_open" ).checked, |
| 246 document.getElementById( "number_of_tabs" ).value, |
| 247 document.getElementById( "time_limit" ).value * time_limit_units.value, |
| 248 function( x ) |
| 249 { |
| 250 progress_message.value = x.active + "/" + x.completed + "/" + x.total; |
| 251 } |
| 252 ); |
| 253 |
| 254 /* |
| 255 * Input |
| 256 */ |
| 257 switch ( document.getElementById( "instructions_tabbox" ).selectedIndex ) |
| 258 { |
| 259 case 0: |
| 260 log_window.log( "Server input not supported at present. Aborted." ); |
| 261 return false; |
| 262 case 1: |
| 263 try |
| 264 { |
| 265 current_session.set_input_file( input_file.value ); |
| 266 } |
| 267 catch ( e ) |
| 268 { |
| 269 log_window.log( e.message ); |
| 270 return false; |
| 271 } |
| 272 break; |
| 273 case 2: |
| 274 var fixed_source = "" |
| 275 + "name: Fixed internal development test\n" |
| 276 + "target:\n" |
| 277 + " - yahoo.com\n" |
| 278 + " - ksl.com\n" |
| 279 + ""; |
| 280 try |
| 281 { |
| 282 current_session.set_input_string( fixed_source ); |
| 283 } |
| 284 catch ( e ) |
| 285 { |
| 286 log_window.log( e.message ); |
| 287 return false; |
| 288 } |
| 289 break; |
| 290 default: |
| 291 log_window.log( "WTF? Unknown input tab. Aborted." ); |
| 292 return false; |
| 293 } |
| 294 |
| 295 /* |
| 296 * Output |
| 297 */ |
| 298 current_session.add_output( log_to_textbox, "YAML" ); |
| 299 switch ( document.getElementById( "storage_tabbox" ).selectedIndex ) |
| 300 { |
| 301 case 0: |
| 302 log_window.log( "Server storage not supported at present. Aborted." ); |
| 303 return false; |
| 304 case 1: |
| 305 try |
| 306 { |
| 307 var output_file_name = |
| 308 current_session.add_output_file( output_directory.value, base_name.val
ue, true, encoding ); |
| 309 } |
| 310 catch ( e ) |
| 311 { |
| 312 log_window.log( e.message ); |
| 313 return false; |
| 314 } |
| 315 log_window.log( "Computed file name = " + output_file_name ); |
| 316 break; |
| 317 case 2: |
| 318 break; |
| 319 default: |
| 320 log_window.log( "WTF? Unknown storage tab. Aborted." ); |
| 321 return false; |
| 322 } |
| 323 |
| 324 current_session.run( crawl_finally, crawl_catch ); |
| 325 |
| 326 // This function is an event handler. |
| 327 return true; |
| 328 } |
| 329 |
| 330 function crawl_catch( ex ) |
| 331 { |
| 332 Cu.reportError( "crawler_ui: Caught crawl exception=" + ex.toString() ); |
| 333 } |
| 334 |
| 335 function crawl_finally() |
| 336 { |
| 337 crawler_ui_log( "Finish" ); |
| 338 log_window.log( "Finish" ); |
| 339 current_session = null; |
| 340 } |
| 341 |
| 342 //------------------------------------------------------- |
| 343 // Crawl_Display |
| 344 //------------------------------------------------------- |
| 345 /** |
| 346 * Constructor for a display object for the crawler. |
| 347 */ |
| 348 function Crawl_Display() |
| 349 { |
| 350 this.display_log = document.getElementById( "display_log" ); |
| 351 this.log_box = document.getElementById( "log_box" ); |
| 352 } |
| 353 |
| 354 Crawl_Display.prototype.log = function( message ) |
| 355 { |
| 356 this.log_box.value += message + "\n"; |
| 357 }; |
| 358 |
| 359 Crawl_Display.prototype.write = function( message ) |
| 360 { |
| 361 if ( this.display_log.checked ) |
| 362 this.log_box.value += message; |
| 363 }; |
OLD | NEW |