-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (129 loc) · 4.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var self = require('sdk/self');
var window = require('sdk/window/utils');
var buttons = require('sdk/ui/button/action');
var tabs = require("sdk/tabs");
var contextMenu = require("sdk/context-menu");
let { Cc, Ci, Cu } = require('chrome');
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/osfile.jsm"); // OS.File things
Cu.import("resource://gre/modules/Downloads.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://gre/modules/Services.jsm");
// Context menu integration
var menuItem = contextMenu.Item({
label: "Save image with tags",
context: contextMenu.SelectorContext("img"),
contentScript: 'self.on("click", function (node, data) {' +
' self.postMessage(node.src);' +
'});',
onMessage: openSaveImageDialog
});
function openSaveImageDialog(imageUri) {
console.log(imageUri);
console.log("taglist: " + getTagList().toString());
dlg = window.open("chrome://pictag/content/saveImage.xul",
"pictag-save-image",
"chrome,centerscreen");
dlg.saveImageWithTags = function(tags, filename) {
saveImageWithTags(imageUri, tags, filename)
}
dlg.getTagList = getTagList;
dlg.addTagToPrefs = addTagToPrefs;
dlg.defaultFilename = filenameFromURI(imageUri);
}
hashCode = function(s){
return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
}
function addHashToFilename(filename, str) {
var dot = filename.indexOf('.');
new_filename = filename.substring(0, dot) + "-" + Math.abs(hashCode(str))
+ filename.substring(dot);
return new_filename;
}
function filenameFromURI(uri) {
var filename = uri.substring(uri.lastIndexOf('/')+1);
return filename;
}
function saveImageWithTags(imageUri, tags, filename) {
var storage = getStorageDir();
filename = addHashToFilename(filename, imageUri);
// find out the first tag to save and skip already processed tags
var firstTag = tags.shift();
Task.spawn(function () {
var firstTagFile = yield tagFilePath(firstTag, filename);
var exists = yield OS.File.exists(firstTagFile);
if (!exists) {
yield Downloads.fetch(imageUri, firstTagFile);
}
copyImageToTags(firstTagFile, tags);
}
).then(null, function (e) {
Cu.reportError(e);
promptService.alert(null, "PicTag error", "Unable to save file, check the console");
});
}
function copyImageToTags(imageFile, tags) {
filename = OS.Path.basename(imageFile);
return Task.spawn(function () {
for (i = 0; i < tags.length; i++) {
var dest = yield tagFilePath(tags[i], filename);
var exists = yield OS.File.exists(dest);
if (!exists) {
yield OS.File.copy(imageFile, dest);
}
}
});
}
function tagFilePath(tag, filename) {
var storage = getStorageDir();
var dir = OS.Path.join(storage, tag);
let promise = OS.File.makeDir(dir);
return promise.then(function () {
return OS.Path.join(dir, filename);
}, Cu.reportError);
}
function getTagList() {
var prefs = Services.prefs.getBranch("extensions.@pictag.");
try {
var taglist_str = prefs.getComplexValue("taglist", Ci.nsISupportsString).data;
if (taglist_str == "") {
return []
} else {
return taglist_str.split(":");
}
} catch (e) {
return [];
}
}
function addTagToPrefs(tag) {
var str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
var prefs = Services.prefs.getBranch("extensions.@pictag.");
var taglist = getTagList();
taglist.push(tag);
str.data = taglist.join(":");
prefs.setComplexValue("taglist", Ci.nsISupportsString, str);
}
function getDefaultStorageDir() {
var default_storage = OS.Path.join(OS.Constants.Path.homeDir, "PicTag");
return default_storage;
}
function getStorageDir() {
var prefs = Services.prefs.getBranch("extensions.@pictag.");
var storage;
try {
storage = prefs.getComplexValue("storagedir", Ci.nsILocalFile);
} catch (e) {
storage = null;
}
if (!storage) {
storage = getDefaultStorageDir();
}
Task.spawn(function () {
yield OS.File.makeDir(storage);
}).then(null, function (e) {
Cu.reportError(e);
promptService.alert(null, "PicTag error", "Unable to create PicTag directory in $HOME");
});
return storage;
}