/SparkleShare/SparkleController.cs

http://github.com/hbons/SparkleShare · C# · 233 lines · 152 code · 54 blank · 27 comment · 9 complexity · ae69235fca96b6f53d61a034d56f48ab MD5 · raw file

  1. // SparkleShare, a collaboration and sharing tool.
  2. // Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.IO;
  20. using System.Runtime.InteropServices;
  21. using System.Text;
  22. using System.Threading;
  23. using Mono.Unix;
  24. using SparkleLib;
  25. namespace SparkleShare {
  26. public class SparkleController : SparkleControllerBase {
  27. public override string PluginsPath {
  28. get {
  29. return SparkleHelpers.CombineMore (Defines.DATAROOTDIR, "sparkleshare", "plugins");
  30. }
  31. }
  32. public SparkleController () : base ()
  33. {
  34. }
  35. // Creates a .desktop entry in autostart folder to
  36. // start SparkleShare automatically at login
  37. public override void EnableSystemAutostart ()
  38. {
  39. string autostart_path = Path.Combine (Environment.GetFolderPath (
  40. Environment.SpecialFolder.ApplicationData), "autostart");
  41. string desktopfile_path = Path.Combine (autostart_path, "sparkleshare.desktop");
  42. if (!Directory.Exists (autostart_path))
  43. Directory.CreateDirectory (autostart_path);
  44. if (!File.Exists (desktopfile_path)) {
  45. TextWriter writer = new StreamWriter (desktopfile_path);
  46. writer.WriteLine ("[Desktop Entry]\n" +
  47. "Type=Application\n" +
  48. "Name=SparkleShare\n" +
  49. "Exec=sparkleshare start\n" +
  50. "Icon=folder-sparkleshare\n" +
  51. "Terminal=false\n" +
  52. "X-GNOME-Autostart-enabled=true\n" +
  53. "Categories=Network");
  54. writer.Close ();
  55. // Give the launcher the right permissions so it can be launched by the user
  56. UnixFileInfo file_info = new UnixFileInfo (desktopfile_path);
  57. file_info.Create (FileAccessPermissions.UserReadWriteExecute);
  58. SparkleHelpers.DebugInfo ("Controller", "Enabled autostart on login");
  59. }
  60. }
  61. // Installs a launcher so the user can launch SparkleShare
  62. // from the Internet category if needed
  63. public override void InstallLauncher ()
  64. {
  65. string apps_path =
  66. new string [] {SparkleConfig.DefaultConfig.HomePath,
  67. ".local", "share", "applications"}.Combine ();
  68. string desktopfile_path = Path.Combine (apps_path, "sparkleshare.desktop");
  69. if (!File.Exists (desktopfile_path)) {
  70. if (!Directory.Exists (apps_path))
  71. Directory.CreateDirectory (apps_path);
  72. TextWriter writer = new StreamWriter (desktopfile_path);
  73. writer.WriteLine ("[Desktop Entry]\n" +
  74. "Type=Application\n" +
  75. "Name=SparkleShare\n" +
  76. "Comment=Share documents\n" +
  77. "Exec=sparkleshare start\n" +
  78. "Icon=folder-sparkleshare\n" +
  79. "Terminal=false\n" +
  80. "Categories=Network;");
  81. writer.Close ();
  82. // Give the launcher the right permissions so it can be launched by the user
  83. UnixFileInfo file_info = new UnixFileInfo (desktopfile_path);
  84. file_info.FileAccessPermissions = FileAccessPermissions.UserReadWriteExecute;
  85. SparkleHelpers.DebugInfo ("Controller", "Created '" + desktopfile_path + "'");
  86. }
  87. }
  88. // Adds the SparkleShare folder to the user's
  89. // list of bookmarked places
  90. public override void AddToBookmarks ()
  91. {
  92. string bookmarks_file_path = Path.Combine (SparkleConfig.DefaultConfig.HomePath, ".gtk-bookmarks");
  93. string sparkleshare_bookmark = "file://" + SparkleConfig.DefaultConfig.FoldersPath + " SparkleShare";
  94. if (File.Exists (bookmarks_file_path)) {
  95. StreamReader reader = new StreamReader (bookmarks_file_path);
  96. string bookmarks = reader.ReadToEnd ();
  97. reader.Close ();
  98. if (!bookmarks.Contains (sparkleshare_bookmark)) {
  99. TextWriter writer = File.AppendText (bookmarks_file_path);
  100. writer.WriteLine ("file://" + SparkleConfig.DefaultConfig.FoldersPath + " SparkleShare");
  101. writer.Close ();
  102. }
  103. } else {
  104. StreamWriter writer = new StreamWriter (bookmarks_file_path);
  105. writer.WriteLine ("file://" + SparkleConfig.DefaultConfig.FoldersPath + " SparkleShare");
  106. writer.Close ();
  107. }
  108. }
  109. // Creates the SparkleShare folder in the user's home folder
  110. public override bool CreateSparkleShareFolder ()
  111. {
  112. if (!Directory.Exists (SparkleConfig.DefaultConfig.FoldersPath)) {
  113. Directory.CreateDirectory (SparkleConfig.DefaultConfig.FoldersPath);
  114. SparkleHelpers.DebugInfo ("Controller", "Created '" + SparkleConfig.DefaultConfig.FoldersPath + "'");
  115. string gvfs_command_path =
  116. new string [] {Path.VolumeSeparatorChar.ToString (),
  117. "usr", "bin", "gvfs-set-attribute"}.Combine ();
  118. // Add a special icon to the SparkleShare folder
  119. if (File.Exists (gvfs_command_path)) {
  120. Process process = new Process ();
  121. process.StartInfo.RedirectStandardOutput = true;
  122. process.StartInfo.UseShellExecute = false;
  123. process.StartInfo.FileName = "gvfs-set-attribute";
  124. // Clear the custom (legacy) icon path
  125. process.StartInfo.Arguments = "-t unset " + SparkleConfig.DefaultConfig.FoldersPath + " metadata::custom-icon";
  126. process.Start ();
  127. process.WaitForExit ();
  128. // Give the SparkleShare folder an icon name, so that it scales
  129. process.StartInfo.Arguments = SparkleConfig.DefaultConfig.FoldersPath + " metadata::custom-icon-name 'folder-sparkleshare'";
  130. process.Start ();
  131. process.WaitForExit ();
  132. }
  133. return true;
  134. }
  135. return false;
  136. }
  137. public override string EventLogHTML {
  138. get {
  139. string html_path = new string [] {Defines.PREFIX, "share",
  140. "sparkleshare", "html", "event-log.html"}.Combine ();
  141. string html = File.ReadAllText (html_path);
  142. string jquery_file_path = new string [] {Defines.PREFIX, "share",
  143. "sparkleshare", "html", "jquery.js"}.Combine ();
  144. string jquery = File.ReadAllText (jquery_file_path);
  145. html = html.Replace ("<!-- $jquery -->", jquery);
  146. return html;
  147. }
  148. }
  149. public override string DayEntryHTML {
  150. get {
  151. string path = new string [] {Defines.PREFIX,
  152. "share", "sparkleshare", "html", "day-entry.html"}.Combine ();
  153. return String.Join (Environment.NewLine, File.ReadAllLines (path));
  154. }
  155. }
  156. public override string EventEntryHTML {
  157. get {
  158. string path = new string [] {Defines.PREFIX,
  159. "share", "sparkleshare", "html", "event-entry.html"}.Combine ();
  160. return String.Join (Environment.NewLine, File.ReadAllLines (path));
  161. }
  162. }
  163. public override void OpenSparkleShareFolder (string subfolder)
  164. {
  165. string folder = Path.Combine (SparkleConfig.DefaultConfig.FoldersPath, subfolder);
  166. Process process = new Process ();
  167. process.StartInfo.FileName = "xdg-open";
  168. process.StartInfo.Arguments = "\"" + folder + "\"";
  169. process.Start ();
  170. }
  171. public override void OpenFile (string url)
  172. {
  173. Process process = new Process ();
  174. process.StartInfo.FileName = "xdg-open";
  175. process.StartInfo.Arguments = url.Replace (" ", "%20");
  176. process.Start ();
  177. }
  178. }
  179. }