Merge lp:~jaapz-b/switchboard-plug-pantheon-shell/fix-1081451 into lp:~elementary-apps/switchboard-plug-pantheon-shell/trunk

Proposed by Jaap Broekhuizen
Status: Merged
Approved by: Victor Martinez
Approved revision: 56
Merged at revision: 55
Proposed branch: lp:~jaapz-b/switchboard-plug-pantheon-shell/fix-1081451
Merge into: lp:~elementary-apps/switchboard-plug-pantheon-shell/trunk
Diff against target: 70 lines (+24/-10)
1 file modified
src/Wallpaper.vala (+24/-10)
To merge this branch: bzr merge lp:~jaapz-b/switchboard-plug-pantheon-shell/fix-1081451
Reviewer Review Type Date Requested Status
Victor Martinez (community) Approve
Review via email: mp+135962@code.launchpad.net
To post a comment you must log in.
56. By Jaap Broekhuizen

Check for valid content type instead of checking the file name for an extension.

Revision history for this message
Victor Martinez (victored) wrote :

Looks perfect to me. The content-type-based file matching is a nice addition!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Wallpaper.vala'
2--- src/Wallpaper.vala 2012-11-21 15:51:52 +0000
3+++ src/Wallpaper.vala 2012-11-23 19:23:22 +0000
4@@ -7,14 +7,28 @@
5 // Helper class for the file IO functions we'll need
6 // Not needed at all, but helpful for organization
7 public class IOHelper : GLib.Object {
8-
9+
10 // Check if the filename has a picture file extension.
11- public static bool is_valid_file_type (string fname) {
12+ public static bool is_valid_file_type (GLib.FileInfo file_info) {
13
14- // Cache a lowe-cased copy of the file name
15- string fname_down = fname.down();
16- // Short-circuit if it's not a picture file extension
17- return (fname_down.has_suffix(".png") || fname_down.has_suffix(".jpeg") || fname_down.has_suffix(".jpg") || fname_down.has_suffix(".gif"));
18+ // Check for correct file type, don't try to load directories and such
19+ if (file_info.get_file_type () != GLib.FileType.REGULAR)
20+ return false;
21+
22+ // Now check if it is an accepted content type
23+ string[] accepted_types = {
24+ "image/jpeg",
25+ "image/png",
26+ "image/tiff",
27+ "image/gif"
28+ };
29+
30+ foreach (var type in accepted_types) {
31+ if (GLib.ContentType.equals (file_info.get_content_type (), type))
32+ return true;
33+ }
34+
35+ return false;
36 }
37
38 // Quickly count up all of the valid wallpapers in the wallpaper folder.
39@@ -24,11 +38,11 @@
40 int count = 0;
41 try {
42 // Get an enumerator for all of the plain old files in the wallpaper folder.
43- var enumerator = wallpaper_folder.enumerate_children(FileAttribute.STANDARD_NAME + "," + FileAttribute.STANDARD_TYPE, 0);
44+ var enumerator = wallpaper_folder.enumerate_children(FileAttribute.STANDARD_NAME + "," + FileAttribute.STANDARD_TYPE + "," + FileAttribute.STANDARD_CONTENT_TYPE, 0);
45 // While there's still files left to count
46 while ((file_info = enumerator.next_file ()) != null) {
47 // If it's a picture file
48- if (file_info.get_file_type() == GLib.FileType.REGULAR && is_valid_file_type(file_info.get_name())) {
49+ if (is_valid_file_type(file_info)) {
50 count++;
51 }
52 }
53@@ -253,7 +267,7 @@
54 folder_combo.set_sensitive (true);
55
56 // Enumerator object that will let us read through the wallpapers asynchronously
57- var e = yield directory.enumerate_children_async (FileAttribute.STANDARD_NAME, 0, Priority.DEFAULT);
58+ var e = yield directory.enumerate_children_async (FileAttribute.STANDARD_NAME + "," + FileAttribute.STANDARD_TYPE + "," + FileAttribute.STANDARD_CONTENT_TYPE, 0, Priority.DEFAULT);
59
60 while (true) {
61 // Grab a batch of 10 wallpapers
62@@ -267,7 +281,7 @@
63 // We're going to add another wallpaper
64 done++;
65 // Skip the file if it's not a picture
66- if (!IOHelper.is_valid_file_type(info.get_name())) {
67+ if (!IOHelper.is_valid_file_type(info)) {
68 continue;
69 }
70 string filename = WALLPAPER_DIR + "/" + info.get_name ();

Subscribers

People subscribed via source and target branches

to all changes: