-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
207 lines (180 loc) · 7.34 KB
/
extension.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
export default class FileSortExtension extends Extension {
constructor(metadata) {
super(metadata);
this._monitors = [];
this._settings = null;
this._folders = [];
this.pendingChanges = {}; // Track changes before applying
}
enable() {
this._settings = this.getSettings();
//this._initializeDefaultFolders();
this._setupMonitors();
console.log('[FileSort] Extension enabled');
// Listen for settings changes
this._settings.connect('changed::folders', () => {
console.log('[FileSort] Settings changed:', this._settings.get_string('folders'));
this._loadConfig();
});
}
disable() {
this._monitors.forEach(monitor => monitor.cancel());
this._monitors = [];
this._settings = null;
this._folders = [];
}
_loadConfig() {
try {
const json = this._settings.get_string('folders');
console.log('[FileSort] Loading config:', json);
this._folders = JSON.parse(json);
// Initialize default config if empty
if (this._folders.length === 0) {
console.log('[FileSort] Initializing default config');
this._folders = [{
path: GLib.get_home_dir() + '/Downloads',
rules: [{
name: 'Example Rule',
filter: {
type: 'extension',
value: ['jpg', 'png']
},
actions: [{
type: 'move',
destination: GLib.get_home_dir() + '/Pictures',
pattern: '{date}-{filename}'
}]
}]
}];
this._saveConfig();
}
} catch(e) {
console.error('[FileSort] Config load error:', e);
}
}
_saveConfig() {
const json = JSON.stringify(this._folders, null, 2);
console.log('[FileSort] Saving config:', json);
this._settings.set_string('folders', json);
}
_setupMonitors() {
this._folders.forEach(folder => {
this._setupMonitor(folder.path);
});
}
_setupMonitor(folderPath) {
try {
const dir = Gio.File.new_for_path(folderPath);
if (!dir.query_exists(null)) return;
const monitor = dir.monitor_directory(
Gio.FileMonitorFlags.NONE,
null
);
monitor.connect('changed', (monitor, file, otherFile, eventType) => {
if (eventType === Gio.FileMonitorEvent.CREATED) {
this._processFile(file, folderPath);
}
});
this._monitors.push(monitor);
} catch(e) {
console.error(`FileSort monitor error: ${e.message}`);
}
}
_processFile(file, baseFolder) {
console.log(`[FileSort] Processing file: ${file.get_path()}`);
const fileName = file.get_basename();
// Skip hidden files and directories
if (fileName.startsWith('.') ||
file.query_file_type(Gio.FileQueryInfoFlags.NONE, null) === Gio.FileType.DIRECTORY) {
return;
}
const folderConfig = this._folders.find(f => f.path === baseFolder);
if (!folderConfig) return;
folderConfig.rules.forEach(rule => {
if (this._matchesFilter(file, rule.filter)) {
this._applyActions(file, rule.actions);
}
});
}
_matchesFilter(file, filter) {
const fileInfo = file.query_info('*', Gio.FileQueryInfoFlags.NONE, null);
switch(filter.type) {
case 'extension':
const ext = file.get_basename().split('.').pop().toLowerCase();
return filter.value.includes(ext);
case 'size':
const fileSize = fileInfo.get_size();
const targetSize = filter.value;
switch(filter.operator) {
case '>': return fileSize > targetSize;
case '<': return fileSize < targetSize;
default: return fileSize === targetSize;
}
case 'date':
const fileDate = fileInfo.get_modification_date_time().to_unix();
const filterDate = GLib.DateTime.new_from_iso8601(filter.date, null).to_unix();
return fileDate >= filterDate && fileDate < filterDate + 86400;
default:
return false;
}
}
_applyActions(file, actions) {
let currentFile = file;
actions.forEach(action => {
try {
switch(action.type) {
case 'move':
console.log(`[FileSort] Moving to: ${action.destination}`);
const destDir = Gio.File.new_for_path(action.destination);
if (!destDir.query_exists(null)) {
destDir.make_directory_with_parents(null);
}
const destPath = GLib.build_filenamev([
action.destination,
currentFile.get_basename()
]);
currentFile.move(
Gio.File.new_for_path(destPath),
Gio.FileCopyFlags.OVERWRITE,
null,
null
);
currentFile = Gio.File.new_for_path(destPath);
break;
case 'rename':
console.log(`[FileSort] Renaming with pattern: ${action.pattern}`);
const newName = this._substituteVariables(currentFile, action.pattern);
const newFile = currentFile.get_parent().get_child(newName);
currentFile.move(
newFile,
Gio.FileCopyFlags.OVERWRITE,
null,
null
);
currentFile = newFile;
break;
case 'delete':
console.log('[FileSort] Deleting file');
currentFile.trash(null);
break;
}
} catch(e) {
console.error(`[FileSort] Action failed: ${e.message}`);
}
});
}
_substituteVariables(file, pattern) {
const now = GLib.DateTime.new_now_local();
const vars = {
'{filename}': file.get_basename().replace(/\.[^.]+$/, ''),
'{date}': now.format('%Y-%m-%d'),
'{time}': now.format('%H-%M'),
'{username}': GLib.get_user_name(),
'{ext}': file.get_basename().split('.').pop()
};
return pattern.replace(/{[^}]+}/g, match => vars[match] || match);
}
}