-
Notifications
You must be signed in to change notification settings - Fork 49
/
webMidori.js
153 lines (126 loc) · 3.86 KB
/
webMidori.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
/* ========================================================================================================
* CREDITS: Code borrowed from SearchBookmarks extension by bmh1980
* at https://extensions.gnome.org/extension/557/search-bookmarks/
* ========================================================================================================
*/
/**
* Copyright (C) 2012 Marcus Habermehl <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
try {
var Gda = imports.gi.Gda;
} catch(e) {
var Gda = null;
}
// External imports
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Shell = imports.gi.Shell;
// Gjs imports
const Lang = imports.lang;
// Internal imports
const Main = imports.ui.main;
const _appSystem = Shell.AppSystem.get_default();
//const _foundApps = _appSystem.initial_search(['midori']);
const _foundApps = _appSystem.lookup_desktop_wmclass('midori');
const _midoriDir = GLib.build_filenamev([GLib.get_user_config_dir(), 'midori']);
var _appInfo = null;
var _bookmarksFile = null;
var _bookmarksMonitor = null;
var _callbackId = null;
var _connection = null;
var bookmarks = [];
function _readBookmarks() {
bookmarks = [];
let result;
if (! _connection) {
try {
_connection = Gda.Connection.open_from_string(
'SQLite', 'DB_DIR=' + _midoriDir + ';DB_NAME=bookmarks', null,
Gda.ConnectionOptions.READ_ONLY);
} catch(e) {
log("ERROR: " + e.message);
return;
}
}
try {
result = _connection.execute_select_command(
'SELECT title, uri FROM bookmarks');
} catch(e) {
log("ERROR: " + e.message);
return;
}
let nRows = result.get_n_rows();
for (let row = 0; row < nRows; row++) {
let name;
let uri;
try {
name = result.get_value_at(0, row);
uri = result.get_value_at(1, row);
} catch(e) {
log("ERROR: " + e.message);
continue;
}
bookmarks.push({
appInfo: _appInfo,
name: name,
score: 0,
uri: uri
});
}
}
function _reset() {
if (_connection) {
_connection.close();
}
_appInfo = null;
_bookmarksFile = null;
_bookmarksMonitor = null;
_callbackId = null;
_connection = null;
bookmarks = [];
}
function init() {
if (! Gda) {
return;
}
if (_foundApps == null || _foundApps.length == 0) {
return;
}
//_appInfo = _foundApps[0].get_app_info();
_appInfo = _foundApps.get_app_info();
_bookmarksFile = Gio.File.new_for_path(GLib.build_filenamev(
[_midoriDir, 'bookmarks.db']));
if (! _bookmarksFile.query_exists(null)) {
_reset();
return;
}
_bookmarksMonitor = _bookmarksFile.monitor_file(
Gio.FileMonitorFlags.NONE, null);
_callbackId = _bookmarksMonitor.connect(
'changed', _readBookmarks.bind(this));
_readBookmarks();
}
function deinit() {
if (_bookmarksMonitor) {
if (_callbackId) {
_bookmarksMonitor.disconnect(_callbackId);
}
_bookmarksMonitor.cancel();
}
_reset();
}