Advertisement
Guest User

get_last_copied_item.vala

a guest
Apr 19th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 1.45 KB | None | 0 0
  1. /*
  2.  * Compile with the following:
  3.  * valac --pkg gee-1.0 --pkg diodon get_last_copied_item.vala
  4.  *
  5.  * libgee-0.8-dev and diodon-dev packages need to be installed
  6.  */
  7.  
  8. using Diodon;
  9.  
  10. public delegate void AsyncBegin(AsyncReadyCallback callback);
  11. public delegate void AsyncFinish(AsyncResult result) throws GLib.Error;
  12.  
  13. public bool wait_for_async(AsyncBegin async_function, AsyncFinish async_finish, int timeout = 200) throws GLib.Error
  14. {
  15.     var loop = new MainLoop(MainContext.default(), true);
  16.     AsyncResult? result = null;
  17.     // Plan the async function
  18.     async_function((o, r) => { result = r; loop.quit(); });
  19.     // Plan timeout
  20.     var t1 = Timeout.add(timeout, () => { loop.quit(); return false; });
  21.     // Run the loop if it was not quit yet.
  22.     if(loop.is_running())
  23.         loop.run();
  24.     // Cancel timer
  25.     Source.remove(t1);
  26.     // Check the outcome
  27.     if(result == null)
  28.         return false;
  29.     async_finish(result);
  30.     return true;
  31. }
  32.    
  33. public static int main(string[] args)
  34. {
  35.     ZeitgeistClipboardStorage storage = new ZeitgeistClipboardStorage();
  36.    
  37.     try {
  38.         wait_for_async(
  39.             cb => storage.get_recent_items.begin(1, null, ClipboardTimerange.ALL, null, cb),
  40.             res => {
  41.                 var items = storage.get_recent_items.end(res);
  42.                 if(items.first() != null) {
  43.                     print(items.first().get_text());
  44.                 }
  45.             } );
  46.     } catch(Error e) {}
  47.    
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement