load_rc: avoid NS_ERROR_FILE_TARGET_DOES_NOT_EXIST in symlink test
[conkeror.git] / modules / rc.js
blob0eae25dff9bd8d1972afc46ad2ff4affc4d913c2
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2010 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 let (default_rc = get_home_directory()) {
11     default_rc.appendRelativePath(".conkerorrc");
12     default_pref("conkeror.rcfile", default_rc.path);
15 function load_rc () {
16     var path;
17     var rcfile = get_pref("conkeror.rcfile");
18     if (rcfile.length == 0)
19         //FIXME: log that the rc is disabled
20         return;
21     path = make_file(rcfile);
22     if (! path.exists()) {
23         if (file_symlink_p(path))
24             dumpln("w: broken symlink, \""+rcfile+"\"");
25         else if (pref_has_user_value("conkeror.rcfile"))
26             dumpln("w: preference conkeror.rcfile is set to "+
27                    "non-existent path, \""+rcfile+"\"");
28         //FIXME: else log that the rc does not exist
29         return;
30     }
31     var files = [];
32     var ret;
33     if (path.isDirectory()) {
34         var entries = path.directoryEntries;
35         while (entries.hasMoreElements()) {
36             var entry = entries.getNext();
37             entry.QueryInterface(Ci.nsIFile);
38             if (entry.leafName.substr(-3).toLowerCase() == '.js')
39                 files.push(entry);
40         }
41         files.sort(function (a, b) {
42             if (a.leafName < b.leafName)
43                 return -1;
44             else if (a.leafName > b.leafName)
45                 return 1;
46             else
47                 return 0;
48         });
49         path.appendRelativePath("a");
50         ret = path.path.substr(0, path.path.length - 1) + "*.js";
51     } else {
52         files.push(path);
53         ret = path.path;
54     }
55     var obs = Cc["@mozilla.org/observer-service;1"]
56         .getService(Ci.nsIObserverService);
57     obs.notifyObservers(null, "startupcache-invalidate", null);
58     for (var i = 0; files[i]; i++) {
59         try {
60             load(files[i]);
61         } catch (e) {
62             dump_error(e);
63         }
64     }
65     //FIXME: log what was loaded instead of returning the value to be
66     //       logged by the caller.
67     return ret;
70 provide("rc");