Advertisement
Guest User

Untitled

a guest
Oct 10th, 2010
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. --- ../udisks-automounter-0.0.2/Makefile    2010-10-05 19:02:14.000000000 +0200
  2. +++ Makefile    2010-10-10 11:32:55.000000000 +0200
  3. @@ -6,10 +6,10 @@ CFLAGS?= -O2
  4.  PREFIX?=/usr/local
  5.  
  6.  udisks-automounter.c udisks-dbus-interfaces.c : udisks-automounter.vala udisks-dbus-interfaces.vala
  7. -   valac --pkg dbus-glib-1 --pkg gio-2.0 -C $^
  8. +   valac --pkg gee-1.0 --pkg dbus-glib-1 --pkg gio-2.0 -C $^
  9.  
  10.  udisks-automounter: udisks-automounter.c udisks-dbus-interfaces.c
  11. -   gcc $(CFLAGS) $(LDFLAGS) -o udisks-automounter `pkg-config --cflags --libs glib-2.0 gio-2.0 dbus-glib-1` $^
  12. +   gcc $(CFLAGS) $(LDFLAGS) -o udisks-automounter `pkg-config --cflags --libs gee-1.0 glib-2.0 gio-2.0 dbus-glib-1` $^
  13.  
  14.  dist: udisks-automounter.c udisks-dbus-interfaces.c
  15.     mkdir udisks-automounter-$(VERSION)
  16. --- ../udisks-automounter-0.0.2/udisks-automounter.vala 2010-10-04 10:45:45.000000000 +0200
  17. +++ udisks-automounter.vala 2010-10-10 11:32:55.000000000 +0200
  18. @@ -19,11 +19,15 @@
  19.  
  20.  using UDisks;
  21.  using DBus;
  22. +using Gee;
  23.  
  24.  const string[] gui_clients = { "Thunar", "nautilus", "pcmanfm", "emelfm2" };
  25.  
  26.  DBus.Connection dbus;
  27.  bool coldplug = true;
  28. +HashMap<string, string> device_map;
  29. +string env_client ;
  30. +bool close_client;
  31.  
  32.  async void
  33.  on_device_added_async(ObjectPath device)
  34. @@ -42,24 +46,74 @@ on_device_added_async(ObjectPath device)
  35.      {
  36.     stdout.printf("  appropriate for auto-mounting\n");
  37.     string path = yield dev.filesystem_mount("", {"auth_no_user_interaction"});
  38. +   device_map[device] = path;
  39.     stdout.printf("  mounted to: %s\n", path);
  40.  
  41.     /* now launch a GUI window on the mount path */
  42.     if (open_client) {
  43.         int pid;
  44. -       foreach (string client in gui_clients) {
  45. -       stdout.printf("  trying to launch %s\n", client);
  46. -       try {
  47. -           if (Process.spawn_async(null, {client, path}, null,
  48. -               SpawnFlags.SEARCH_PATH, null, out pid))
  49. -           stdout.printf("  success!\n");
  50. -           break;
  51. -       } catch { /* ignore, try next one */ }
  52. +       Environment.set_variable ("ACTION", "MOUNT", true);
  53. +            if (env_client == null)
  54. +       {
  55. +           foreach (string client in gui_clients) {
  56. +           stdout.printf("  trying to launch %s\n", client);
  57. +           try {
  58. +               if (Process.spawn_async(null, {client, path}, null,
  59. +                   SpawnFlags.SEARCH_PATH, null, out pid))
  60. +               stdout.printf("  success!\n");
  61. +               break;
  62. +           } catch { /* ignore, try next one */ }
  63. +           }
  64. +       }
  65. +       else
  66. +       {
  67. +       if (Process.spawn_async(null, {env_client, path}, null,
  68. +           SpawnFlags.SEARCH_PATH, null, out pid))
  69. +           stdout.printf("  success!\n");
  70. +       }
  71. +   }
  72. +    }
  73. +}
  74. +
  75. +async void
  76. +on_device_removed_async(ObjectPath device)
  77. +{
  78. +    stdout.printf("Device removed: %s \n", device);
  79. +    if ( device_map.has_key(device) )
  80. +    {
  81. +        string path = device_map[device];
  82. +        if (device_map.unset(device))
  83. +        {
  84. +             stdout.printf("Device removed from List: %s \n", device);
  85. +        }
  86. +
  87. +        /* now launch a GUI window on the mount path */
  88. +        if (close_client) {
  89. +            int pid;
  90. +       Environment.set_variable ("ACTION", "UNMOUNT", true);
  91. +            if (env_client == null)
  92. +       {
  93. +           foreach (string client in gui_clients) {
  94. +               stdout.printf("  trying to launch %s\n", client);
  95. +               try {
  96. +                   if (Process.spawn_async(null, {client, path}, null,
  97. +                           SpawnFlags.SEARCH_PATH, null, out pid))
  98. +                       stdout.printf("  success!\n");
  99. +                   break;
  100. +               } catch { /* ignore, try next one */ }
  101. +                   }
  102. +            }
  103. +       else
  104. +       {
  105. +       if (Process.spawn_async(null, {env_client, path}, null,
  106. +           SpawnFlags.SEARCH_PATH, null, out pid))
  107. +           stdout.printf("  success!\n");
  108.         }
  109.     }
  110.      }
  111.  }
  112.  
  113. +
  114.  void
  115.  on_device_added(ObjectPath device)
  116.  {
  117. @@ -69,7 +123,7 @@ on_device_added(ObjectPath device)
  118.  void
  119.  on_device_removed(ObjectPath device)
  120.  {
  121. -    stdout.printf("Device removed: %s\n", device);
  122. +    on_device_removed_async.begin(device);
  123.  }
  124.  
  125.  int
  126. @@ -89,6 +143,27 @@ main(string[] args)
  127.  
  128.      stdout.printf("Connected to UDisks %s\n", udisks.daemon_version);
  129.  
  130. +    /* initialize handled devices map */
  131. +    device_map = new HashMap<string, string> ();
  132. +
  133. +    /* try to read client and if action to be send from ENV */
  134. +    if (Environment.get_variable("UDISK_AUTOMOUNTER_CLIENT") != null)
  135. +    {
  136. +        env_client = Environment.get_variable("UDISK_AUTOMOUNTER_CLIENT");
  137. +    }
  138. +
  139. +    if (Environment.get_variable("UDISK_AUTOMOUNTER_ON_CLOSE") != null)
  140. +    {
  141. +        if  (Environment.get_variable("UDISK_AUTOMOUNTER_ON_CLOSE") == "yes")
  142. +   {
  143. +       close_client = true ;
  144. +   }
  145. +   else
  146. +   {
  147. +       close_client = false;
  148. +   }
  149. +    }
  150. +
  151.      /* Coldplug */
  152.      foreach (ObjectPath o in udisks.enumerate_devices())
  153.     on_device_added_async (o);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement