Merge lp:~voldyman/switchboard-plug-default-applications/rework into lp:switchboard-plug-default-applications

Proposed by Akshay Shekher
Status: Needs review
Proposed branch: lp:~voldyman/switchboard-plug-default-applications/rework
Merge into: lp:switchboard-plug-default-applications
Diff against target: 313 lines (+156/-77)
5 files modified
LICENSE (+1/-1)
src/CMakeLists.txt (+3/-2)
src/Utils.vala (+1/-1)
src/Worker.vala (+84/-0)
src/default-plug.vala (+67/-73)
To merge this branch: bzr merge lp:~voldyman/switchboard-plug-default-applications/rework
Reviewer Review Type Date Requested Status
Julián Unrrein (community) code style Needs Fixing
Review via email: mp+193734@code.launchpad.net

Description of the change

the new branch updated the license year to 2013 and

switches form creating new threads to using a thread pool, which forces reuse of threads (making new threads is expensive)

To post a comment you must log in.
Revision history for this message
Julián Unrrein (junrrein) wrote :

Diff line 125, 128: Insert a space before the opening parenthesis.

Diff line 114: Insert a colon inside the warning, after "ocurred".

Diff line 128: Would it be better to use "message" or "warning"?

review: Needs Fixing (code style)

Unmerged revisions

65. By Akshay Shekher

Updated License year & Switched to thread pool instead of making new threads for each change.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'LICENSE'
--- LICENSE 2013-02-12 20:07:40 +0000
+++ LICENSE 2013-11-04 07:13:22 +0000
@@ -1,4 +1,4 @@
1Copyright (C) 2011-2012 elementary Developers1Copyright (C) 2011-2013 elementary Developers
2This program is free software: you can redistribute it and/or modify it2This program is free software: you can redistribute it and/or modify it
3under the terms of the GNU Lesser General Public License version 3, as published3under the terms of the GNU Lesser General Public License version 3, as published
4by the Free Software Foundation.4by the Free Software Foundation.
55
=== modified file 'src/CMakeLists.txt'
--- src/CMakeLists.txt 2013-02-16 05:53:32 +0000
+++ src/CMakeLists.txt 2013-11-04 07:13:22 +0000
@@ -9,13 +9,14 @@
99
10find_package (Vala REQUIRED)10find_package (Vala REQUIRED)
11include (ValaVersion)11include (ValaVersion)
12ensure_vala_version ("0.16.0" MINIMUM)12ensure_vala_version ("0.18.0" MINIMUM)
1313
14include (ValaPrecompile)14include (ValaPrecompile)
15# Add all your vala files and requires packages to the List below to include them in the build15# Add all your vala files and requires packages to the List below to include them in the build
16vala_precompile (VALA_C16vala_precompile (VALA_C
17 default-plug.vala17 default-plug.vala
18 utils.vala18 Utils.vala
19 Worker.vala
19 ${CMAKE_CURRENT_BINARY_DIR}/config.vala20 ${CMAKE_CURRENT_BINARY_DIR}/config.vala
20PACKAGES21PACKAGES
21 pantheon22 pantheon
2223
=== renamed file 'src/utils.vala' => 'src/Utils.vala'
--- src/utils.vala 2013-02-16 05:35:14 +0000
+++ src/Utils.vala 2013-11-04 07:13:22 +0000
@@ -1,7 +1,7 @@
1/***1/***
2 BEGIN LICENSE2 BEGIN LICENSE
33
4 Copyright (C) 2011-2012 elementary Developers4 Copyright (C) 2011-2013 elementary Developers
5 This program is free software: you can redistribute it and/or modify it5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License version 3, as published6 under the terms of the GNU Lesser General Public License version 3, as published
7 by the Free Software Foundation.7 by the Free Software Foundation.
88
=== added file 'src/Worker.vala'
--- src/Worker.vala 1970-01-01 00:00:00 +0000
+++ src/Worker.vala 2013-11-04 07:13:22 +0000
@@ -0,0 +1,84 @@
1/***
2 BEGIN LICENSE
3
4 Copyright (C) 2011-2013 elementary Developers
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License version 3, as published
7 by the Free Software Foundation.
8
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranties of
11 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12 PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program. If not, see <http://www.gnu.org/licenses/>
16
17 END LICENSE
18 Written By: Akshay Shekher <voldyman666@gmail.com>
19
20***/
21
22public class Worker {
23
24 GLib.AppInfo app_from;
25 GLib.AppInfo app_to;
26 string app_type;
27
28 public Worker (GLib.AppInfo change_from, GLib.AppInfo change_to,
29 string item_type) {
30
31 this.app_from = change_from;
32 this.app_to = change_to;
33 app_type = item_type;
34 }
35
36 public void run () {
37 change_default ();
38 }
39
40 private void change_default () {
41
42 map_types_to_app (get_types_for_app (app_type), app_to);
43
44 /* the code below implements ->
45 string[] old_types = app_from.get_supported_types ();
46 map_types_to_app (old_types, app_to);
47
48 The function AppInfo.get_supported_types () is not present in the current glib
49 and will be used when we switch to the newer version.
50 */
51 var old_app_keyfile = new KeyFile ();
52
53 try {
54 old_app_keyfile.load_from_file (((DesktopAppInfo) app_from).filename,
55 KeyFileFlags.NONE);
56 } catch (Error e) {
57 warning ("An error occured %s".printf (e.message));
58 }
59
60 string oldapp_types;
61
62 try {
63 oldapp_types = old_app_keyfile.get_string (KeyFileDesktop.GROUP,
64 KeyFileDesktop.KEY_MIME_TYPE);
65 } catch (Error e) {
66 warning ("An error occured %s".printf (e.message));
67 oldapp_types = "";
68 }
69 // end block
70
71 map_types_to_app (oldapp_types.split (","), app_to);
72 }
73
74 private static void map_types_to_app (string[] types, GLib.AppInfo app) {
75 try {
76 for (int i=0; i < types.length; i++) {
77 app.set_as_default_for_type(types[i]);
78 }
79 } catch (GLib.Error e) {
80 stdout.printf("Error: %s\n", e.message);
81 }
82
83 }
84}
0\ No newline at end of file85\ No newline at end of file
186
=== modified file 'src/default-plug.vala'
--- src/default-plug.vala 2013-04-28 09:16:45 +0000
+++ src/default-plug.vala 2013-11-04 07:13:22 +0000
@@ -1,7 +1,7 @@
1/***1/***
2 BEGIN LICENSE2 BEGIN LICENSE
33
4 Copyright (C) 2011-2012 elementary Developers4 Copyright (C) 2011-2013 elementary Developers
5 This program is free software: you can redistribute it and/or modify it5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License version 3, as published6 under the terms of the GNU Lesser General Public License version 3, as published
7 by the Free Software Foundation.7 by the Free Software Foundation.
@@ -65,14 +65,29 @@
65 GLib.AppInfo te_old;65 GLib.AppInfo te_old;
66 GLib.AppInfo fb_old;66 GLib.AppInfo fb_old;
6767
68 /* make a thread pool to avoid blocking the UI when processing */
69 GLib.ThreadPool<Worker> pool;
70
68 public DefaultPlug () {71 public DefaultPlug () {
6972
73 try {
74 pool = new ThreadPool<Worker>.with_owned_data (worker_func, 3, false);
75 } catch (ThreadError e) {
76 critical ("Unable to create a thread pool: %s", e.message);
77 }
78
79 setup_ui ();
80 cache_apps ();
81 connect_signals ();
82 }
83
84 private void setup_ui () {
70 var grid = new Gtk.Grid ();85 var grid = new Gtk.Grid ();
71 grid.halign = Gtk.Align.CENTER;86 grid.halign = Gtk.Align.CENTER;
72 grid.set_column_homogeneous (false);87 grid.set_column_homogeneous (false);
73 grid.set_row_spacing (6);88 grid.set_row_spacing (6);
74 grid.set_column_spacing (10);89 grid.set_column_spacing (10);
75 90
76 //space between the two columns91 //space between the two columns
77 int margin_columns = 40;92 int margin_columns = 40;
78 grid.margin_left = margin_columns;93 grid.margin_left = margin_columns;
@@ -132,77 +147,56 @@
132 grid.attach (fb_label, 2, 3, 1, 1);147 grid.attach (fb_label, 2, 3, 1, 1);
133 grid.attach (fb_chooser, 3, 3, 1, 1);148 grid.attach (fb_chooser, 3, 3, 1, 1);
134149
135 cache_apps ();
136
137 wb_chooser.changed.connect ( () => run_in_thread ( () => {
138 change_default(wb_old, wb_chooser.get_app_info (), "web_browser");
139 return null;
140 }));
141
142 ec_chooser.changed.connect ( () => run_in_thread ( () => {
143 change_default(ec_old, ec_chooser.get_app_info (), "email_client");
144 return null;
145 }));
146
147 c_chooser.changed.connect ( () => run_in_thread ( () => {
148 change_default(c_old, c_chooser.get_app_info (), "calendar");
149 return null;
150 }));
151
152 vp_chooser.changed.connect ( () => run_in_thread ( () => {
153 change_default(vp_old, vp_chooser.get_app_info (), "video_player");
154 return null;
155 }));
156
157 mp_chooser.changed.connect ( () => run_in_thread ( () => {
158 change_default(mp_old, mp_chooser.get_app_info (), "music_player");
159 return null;
160 }));
161
162 iv_chooser.changed.connect ( () => run_in_thread ( () => {
163 change_default (iv_old, iv_chooser.get_app_info (), "image_viewer");
164 return null;
165 }));
166 te_chooser.changed.connect ( () => run_in_thread ( () => {
167 change_default(te_old, te_chooser.get_app_info (), "text_editor");
168 return null;
169 }));
170 fb_chooser.changed.connect ( () => run_in_thread ( () => {
171 change_default(fb_old, fb_chooser.get_app_info (), "file_browser");
172 return null;
173 }));
174
175 add (grid);150 add (grid);
176151 }
177 }152
178 private void run_in_thread (ThreadFunc<void*> func) {153
179 new Thread<void*> (null, func);154 private void connect_signals () {
180 }155
181 public void change_default (GLib.AppInfo old_app, GLib.AppInfo new_app, string item_type) {156 wb_chooser.changed.connect ( () => {
182 map_types_to_app (get_types_for_app (item_type), new_app);157 add_task (wb_old, wb_chooser.get_app_info (), "web_browser");
183158 });
184 /* the code below implements ->159
185 string[] old_types = old_app.get_supported_types ();160 ec_chooser.changed.connect ( () => {
186 map_types_to_app (old_types, new_app);161 add_task (ec_old, ec_chooser.get_app_info (), "email_client");
187 The function AppInfo.get_supported_types () is not present in the current glib162 });
188 and will be used when we switch to the newer version.163
189 */164 c_chooser.changed.connect ( () => {
190 var old_app_keyfile = new KeyFile ();165 add_task (c_old, c_chooser.get_app_info (), "calendar");
191 try {166 });
192 old_app_keyfile.load_from_file (((DesktopAppInfo) old_app).filename, KeyFileFlags.NONE);167
193 } catch (Error e) {168 vp_chooser.changed.connect ( () => {
194 warning ("An error occured %s".printf (e.message));169 add_task (vp_old, vp_chooser.get_app_info (), "video_player");
195 }170 });
196 string oldapp_types;171
197 try {172 mp_chooser.changed.connect ( () => {
198 oldapp_types = old_app_keyfile.get_string (KeyFileDesktop.GROUP, KeyFileDesktop.KEY_MIME_TYPE);173 add_task (mp_old, mp_chooser.get_app_info (), "music_player");
199 } catch (Error e) {174 });
200 warning ("An error occured %s".printf (e.message));175
201 oldapp_types = "";176 iv_chooser.changed.connect ( () => {
202 }177 add_task (iv_old, iv_chooser.get_app_info (), "image_viewer");
203 //end block178 });
204 map_types_to_app (oldapp_types.split (","), new_app);179
205180 te_chooser.changed.connect ( () => {
181 add_task (te_old, te_chooser.get_app_info (), "text_editor");
182 });
183
184 fb_chooser.changed.connect ( () => {
185 add_task (fb_old, fb_chooser.get_app_info (), "file_browser");
186 });
187 }
188
189 private void add_task (GLib.AppInfo app_from, GLib.AppInfo app_to,
190 string item_type) {
191 try {
192 pool.add (new Worker (app_from, app_to, item_type));
193 } catch (ThreadError e) {
194 warning ("Could not add the worker to pool: %s", e.message);
195 }
196 }
197
198 private void worker_func (owned Worker task) {
199 task.run ();
206 cache_apps ();200 cache_apps ();
207 }201 }
208202
@@ -210,7 +204,7 @@
210 /* Cache the AppInfo of the old default apps */204 /* Cache the AppInfo of the old default apps */
211 wb_old = wb_chooser.get_app_info ();205 wb_old = wb_chooser.get_app_info ();
212 ec_old = ec_chooser.get_app_info ();206 ec_old = ec_chooser.get_app_info ();
213 c_old = c_chooser.get_app_info ();207 c_old = c_chooser.get_app_info ();
214 vp_old = vp_chooser.get_app_info ();208 vp_old = vp_chooser.get_app_info ();
215 mp_old = mp_chooser.get_app_info ();209 mp_old = mp_chooser.get_app_info ();
216 iv_old = iv_chooser.get_app_info ();210 iv_old = iv_chooser.get_app_info ();

Subscribers

People subscribed via source and target branches

to all changes: