Merge lp:~renatofilho/unity-lens-video/unity-lens-videos-dconf into lp:unity-lens-video

Proposed by Renato Araujo Oliveira Filho
Status: Superseded
Proposed branch: lp:~renatofilho/unity-lens-video/unity-lens-videos-dconf
Merge into: lp:unity-lens-video
Diff against target: 197 lines (+98/-33)
3 files modified
com.canonical.Unity.VideosLens.gschema.xml (+9/-0)
setup.py (+1/-0)
src/unity-lens-video (+88/-33)
To merge this branch: bzr merge lp:~renatofilho/unity-lens-video/unity-lens-videos-dconf
Reviewer Review Type Date Requested Status
Unity Videos lens Pending
Review via email: mp+100709@code.launchpad.net

This proposal has been superseded by a proposal from 2012-04-03.

Description of the change

Implemented support for videos search in a pre-configure folder.

Due the async call of updatedb command some changes was necessary to avoid problems with the cache database.

Created a temporary cache file for each directory;
The cache file is update in each directory change instead of every search;

To test it you need install and compile the gconf scheme:
  Install: python setup.py install
  Compile schema: glib-compile-schemas /usr/share/glib-2.0/schemas/

To post a comment you must log in.
70. By Renato Araujo Oliveira Filho

Implemented support for recusrsive folder change.
Cache file name now is based on folder name.

71. By Renato Araujo Oliveira Filho

Fixed search function to work with new file monitor API.

72. By Renato Araujo Oliveira Filho

Fixed recursive monitor when moving a folder.

Unmerged revisions

72. By Renato Araujo Oliveira Filho

Fixed recursive monitor when moving a folder.

71. By Renato Araujo Oliveira Filho

Fixed search function to work with new file monitor API.

70. By Renato Araujo Oliveira Filho

Implemented support for recusrsive folder change.
Cache file name now is based on folder name.

69. By Renato Araujo Oliveira Filho

Implemented support for search videos in a pre-configured folder;

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'com.canonical.Unity.VideosLens.gschema.xml'
2--- com.canonical.Unity.VideosLens.gschema.xml 1970-01-01 00:00:00 +0000
3+++ com.canonical.Unity.VideosLens.gschema.xml 2012-04-03 22:50:25 +0000
4@@ -0,0 +1,9 @@
5+<schemalist>
6+ <schema path="/desktop/unity/lenses/videos/" id="com.canonical.Unity.VideosLens" gettext-domain="unity-lens-videos">
7+ <key type="as" name="extra-folders">
8+ <default>[]</default>
9+ <summary>Extra folders used for find for videos</summary>
10+ <description>Key for storing extra folders where the video lens will find for videos. </description>
11+ </key>
12+ </schema>
13+</schemalist>
14
15=== modified file 'setup.py'
16--- setup.py 2012-03-20 22:00:58 +0000
17+++ setup.py 2012-04-03 22:50:25 +0000
18@@ -12,5 +12,6 @@
19 data_files=[
20 ('lib/unity-lens-video', ['src/unity-lens-video']),
21 ('share/dbus-1/services', ['unity-lens-video.service']),
22+ ('share/glib-2.0/schemas', ['com.canonical.Unity.VideosLens.gschema.xml']),
23 ], cmdclass={"build": build_extra.build_extra,
24 "build_i18n": build_i18n.build_i18n,})
25
26=== modified file 'src/unity-lens-video'
27--- src/unity-lens-video 2012-04-03 14:37:36 +0000
28+++ src/unity-lens-video 2012-04-03 22:50:25 +0000
29@@ -25,6 +25,7 @@
30 from zeitgeist.client import ZeitgeistClient
31 from zeitgeist import client, datamodel
32 import time
33+import tempfile
34
35 #pylint: disable=E0611
36 from gi.repository import (
37@@ -85,10 +86,10 @@
38 LOCAL_VIDEOS = _("My Videos")
39
40 BUS_NAME = "net.launchpad.lens.video"
41+DCONF_VIDEO_KEY = "com.canonical.Unity.VideosLens"
42 FOLDER = GLib.get_user_special_dir(GLib.USER_DIRECTORY_VIDEOS)
43 HOME_FOLDER = GLib.get_home_dir()
44 CACHE = "%s/unity-lens-video" % GLib.get_user_cache_dir()
45-DB = "videos.db"
46 Q = []
47 Q_MAX = 3
48 ZG = ZeitgeistClient()
49@@ -98,8 +99,7 @@
50
51 """Creation of a lens with a local scope."""
52
53- def __init__(self):
54-
55+ def __init__(self):
56 #Create the lens
57 self._lens = Unity.Lens.new("/net/launchpad/lens/video", "video")
58 self._lens.props.search_hint = HINT
59@@ -126,8 +126,8 @@
60
61 filters = []
62 self._lens.props.filters = filters
63-
64-
65+
66+
67 # Create the scope
68 self._scope = Unity.Scope.new("/net/launchpad/lens/video/main")
69 self._scope.search_in_global = True
70@@ -140,6 +140,62 @@
71 self._lens.add_local_scope(self._scope)
72 self._lens.export()
73
74+ #Load extra folders
75+ self._scan_folders = [FOLDER]
76+ try:
77+ settings = Gio.Settings.new(DCONF_VIDEO_KEY)
78+ self._scan_folders.extend(settings.get_strv("extra-folders"))
79+ except GLib.GError:
80+ print "Can not read extra-folders configuration"
81+
82+ self.start_dir_monitors()
83+
84+ def update_file_database(self, folder, cache_name):
85+ """Update video cache"""
86+ if (folder != HOME_FOLDER):
87+ try:
88+ GLib.spawn_async(['/usr/bin/updatedb', '-o', cache_name,
89+ '-l', '0', '-U', folder])
90+ except GLib.GError:
91+ print "Can't create the database, will retry."
92+
93+
94+ def on_directory_changed(self, filemonitor, g_file, other_file, event_type, folder):
95+ """This function will be called for any change on scan folder"""
96+ if event_type == Gio.FileMonitorEvent.CREATED:
97+ file_type = g_file.query_file_type(Gio.FileQueryInfoFlags.NONE, None)
98+ #If is a file change wait for CHANGES_DONE_HINT event (wait copy or move file)
99+ if file_type is Gio.FileType.REGULAR:
100+ return
101+
102+ self.update_file_database(folder, filemonitor._cache_file.name)
103+
104+
105+ def start_dir_monitors(self):
106+ """Check for the existence of the cache folder, create it if needed,
107+ and start directory monitors. """
108+ if not Gio.file_new_for_path(CACHE).query_exists(None):
109+ Gio.file_new_for_path(CACHE).make_directory(None)
110+
111+ self._monitors = []
112+ for folder in self._scan_folders:
113+ g_file = Gio.file_new_for_path(folder)
114+ if g_file.query_exists(None):
115+ file_type = g_file.query_file_type(Gio.FileQueryInfoFlags.NONE, None)
116+
117+ if file_type is Gio.FileType.DIRECTORY:
118+ monitor = g_file.monitor_directory(Gio.FileMonitorFlags.NONE, None)
119+
120+ if monitor:
121+ # Create a temporary file for each monitor and keep that
122+ # alive until the monitor get destroyed
123+ monitor._cache_file = tempfile.NamedTemporaryFile(suffix='.db', prefix='video', dir=CACHE)
124+ monitor._folder = folder
125+ self._monitors.append(monitor)
126+ monitor.connect("changed", self.on_directory_changed, folder)
127+ # Initialize database
128+ self.update_file_database(folder, monitor._cache_file.name)
129+
130 def on_filtering_changed(self, *_):
131 """Run another search when a filter change is notified."""
132 self._scope.queue_search_changed(Unity.SearchType.DEFAULT)
133@@ -176,37 +232,36 @@
134 self.update_results_model(search_string, model, 'lens', cancellable, search_status)
135
136 def update_results_model(self, search, model, cat, cancellable, search_status):
137- """Check for the existence of the cache folder, create it if needed,
138- and run the search method."""
139- if not Gio.file_new_for_path(CACHE).query_exists(None):
140- Gio.file_new_for_path(CACHE).make_directory(None)
141- if FOLDER != HOME_FOLDER:
142- try:
143- GLib.spawn_async(['/usr/bin/updatedb', '-o', CACHE+'/'+DB,
144- '-l', '0', '-U', FOLDER])
145- except GLib.GError:
146- print "Can't create the database, will retry."
147-
148- if self.is_file(CACHE+'/'+DB):
149- try:
150- results = GLib.spawn_sync(None,
151- ['/usr/bin/locate',
152- '-id', CACHE+'/'+DB,
153- FOLDER+'*'+search+'*' ],
154- None, 0, None, None)
155- except GLib.GError:
156- results = None
157+ results = ""
158+ for monitor in self._monitors:
159+ if monitor._cache_file:
160+ locate_results = None
161+ try:
162+ #locale accept more then one database as argument but since
163+ #the pattern used has the folder name we need to call this
164+ #for each folder
165+ locate_results = GLib.spawn_sync(None,
166+ ['/usr/bin/locate',
167+ '-id', monitor._cache_file.name,
168+ monitor._folder+'*'+search+'*' ],
169+ None, 0, None, None)
170+ except GLib.GError:
171+ locate_results = None
172+ else:
173+ # spawn_sync returns bool, stdout, stderr, exit_status
174+ if locate_results[3] == 0:
175+ locate_results = locate_results[1]
176+ else:
177+ locate_results = None
178 else:
179- # spawn_sync returns bool, stdout, stderr, exit_status
180- if results[3] == 0:
181- results = results[1]
182- else:
183- results = None
184- else:
185- results = None
186+ locate_results = None
187+
188+ if locate_results:
189+ results += locate_results
190+
191 result_list = []
192 blacklist = self.get_blacklist ()
193- if results:
194+ if len(results) > 0:
195 video_counter = 0
196 print len(results.split('\n'))
197 for video in results.split('\n'):

Subscribers

People subscribed via source and target branches