~minos-archive/minos/firefox-minos-settings

« back to all changes in this revision

Viewing changes to firefox27/firefox/h5xyzl6e.default/extensions/lazarus@interclue.com/chrome/content/file.js

  • Committer: Javier Lopez
  • Date: 2015-07-08 01:10:19 UTC
  • Revision ID: git-v1:d8c3500bae00a8a67d74f8f96235ec11d75231be
rename mozilla-minos-settings -> firefox-minos-settings

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
Lazarus.file = {};
 
3
 
 
4
//cache the file separator to prevent multiple lookups
 
5
Lazarus.file.FILE_SEPARATOR = null;
 
6
 
 
7
/**
 
8
* delete a file or folder
 
9
*/
 
10
Lazarus.file.kill = function(path){
 
11
    var file = Lazarus.file.getFile(path);
 
12
    if (file.exists()){
 
13
        file.remove(true);
 
14
    }
 
15
}
 
16
 
 
17
/**
 
18
* return TRUE if file exists and is a file or a directory
 
19
*/
 
20
Lazarus.file.exists = function(path){
 
21
    return Lazarus.file.getFile(path).exists();
 
22
}
 
23
 
 
24
/**
 
25
* return an nsIFile object for the given path
 
26
*/
 
27
Lazarus.file.getFile = function(path){
 
28
    path = Lazarus.file.fixPath(path);
 
29
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
 
30
    file.initWithPath(path);
 
31
    return file;
 
32
}
 
33
 
 
34
/**
 
35
* fix slashes in a path to match the OS
 
36
*/
 
37
Lazarus.file.fixPath = function(path){
 
38
 
 
39
    //allow the path to start with special paths
 
40
    var m = path.match(/^%(\w+)%/);
 
41
    if (m){
 
42
        path = path.replace(m[0], Lazarus.file.getSpecialDir(m[1]));
 
43
    }
 
44
 
 
45
    if (!Lazarus.file.FILE_SEPARATOR){
 
46
        var profilePath = Lazarus.file.getSpecialDir("profile");
 
47
        Lazarus.file.FILE_SEPARATOR = (profilePath.indexOf("/") > -1) ? "/" : "\\";
 
48
    }
 
49
    return path.replace(/[\/\\]/g, Lazarus.file.FILE_SEPARATOR);
 
50
}
 
51
 
 
52
/**
 
53
* return the path to a special directory
 
54
* directory names can be given in mozilla speak (eg "ProfD") or english ("profile")
 
55
*/
 
56
Lazarus.file.getSpecialDir = function(dirName){
 
57
    
 
58
    switch (dirName.toLowerCase()){
 
59
        case "profd":
 
60
        case "profile":
 
61
            dirName = "ProfD";
 
62
            break;
 
63
        
 
64
        default:
 
65
            //assume programmer knows what he's doing and is asking for a valid directory.
 
66
    }
 
67
    
 
68
    return Components.classes["@mozilla.org/file/directory_service;1"]
 
69
         .getService(Components.interfaces.nsIProperties)
 
70
         .get(dirName, Components.interfaces.nsIFile).path;
 
71
}
 
72
 
 
73
/**
 
74
* copy a file from src to dest
 
75
*/
 
76
Lazarus.file.copy = function(src, dest, overwrite){
 
77
    var srcFile = Lazarus.file.getFile(src);
 
78
    dest = Lazarus.file.fixPath(dest);
 
79
    
 
80
    var m = dest.match(/(.+)[\\\/]([^\\\/]+$)/);
 
81
    var destFilename = m ? m[2] : dest;
 
82
    var destPath = m ? m[1] : srcFile.parent.path;
 
83
    
 
84
    var destDir = Lazarus.file.getFile(destPath);
 
85
    
 
86
    if (overwrite){
 
87
      Lazarus.file.kill(dest);
 
88
    }
 
89
    srcFile.copyTo(destDir, destFilename);
 
90
}
 
91
 
 
92
/**
 
93
* move a file from src to dest
 
94
*/
 
95
Lazarus.file.move = function(src, dest, overwrite){
 
96
    var srcFile = Lazarus.file.getFile(src);
 
97
    dest = Lazarus.file.fixPath(dest);
 
98
    
 
99
    var m = dest.match(/(.+)[\\\/]([^\\\/]+$)/);
 
100
    var destFilename = m ? m[2] : dest;
 
101
    var destPath = m ? m[1] : srcFile.parent.path;
 
102
    
 
103
    var destDir = Lazarus.file.getFile(destPath);
 
104
    if (overwrite){
 
105
      Lazarus.file.kill(dest);
 
106
    }
 
107
    srcFile.moveTo(destDir, destFilename);
 
108
}