PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17
 18using System;
 19using System.Collections.Generic;
 20using System.Diagnostics;
 21using System.IO;
 22using System.Runtime.InteropServices;
 23using System.Text;
 24using System.Threading;
 25
 26using Mono.Unix;
 27using SparkleLib;
 28
 29namespace SparkleShare {
 30
 31    public class SparkleController : SparkleControllerBase {
 32
 33
 34        public override string PluginsPath {
 35            get {
 36                return SparkleHelpers.CombineMore (Defines.DATAROOTDIR, "sparkleshare", "plugins");
 37            }
 38        }
 39
 40
 41        public SparkleController () : base ()
 42        {
 43        }
 44
 45
 46        // Creates a .desktop entry in autostart folder to
 47        // start SparkleShare automatically at login
 48        public override void EnableSystemAutostart ()
 49        {
 50            string autostart_path = Path.Combine (Environment.GetFolderPath (
 51                Environment.SpecialFolder.ApplicationData), "autostart");
 52
 53            string desktopfile_path = Path.Combine (autostart_path, "sparkleshare.desktop");
 54
 55            if (!Directory.Exists (autostart_path))
 56                Directory.CreateDirectory (autostart_path);
 57
 58            if (!File.Exists (desktopfile_path)) {
 59                TextWriter writer = new StreamWriter (desktopfile_path);
 60                writer.WriteLine ("[Desktop Entry]\n" +
 61                                  "Type=Application\n" +
 62                                  "Name=SparkleShare\n" +
 63                                  "Exec=sparkleshare start\n" +
 64                                  "Icon=folder-sparkleshare\n" +
 65                                  "Terminal=false\n" +
 66                                  "X-GNOME-Autostart-enabled=true\n" +
 67                                  "Categories=Network");
 68                writer.Close ();
 69
 70                // Give the launcher the right permissions so it can be launched by the user
 71                UnixFileInfo file_info = new UnixFileInfo (desktopfile_path);
 72                file_info.Create (FileAccessPermissions.UserReadWriteExecute);
 73
 74                SparkleHelpers.DebugInfo ("Controller", "Enabled autostart on login");
 75            }
 76        }
 77        
 78
 79        // Installs a launcher so the user can launch SparkleShare
 80        // from the Internet category if needed
 81        public override void InstallLauncher ()
 82        {
 83            string apps_path = 
 84                new string [] {SparkleConfig.DefaultConfig.HomePath,
 85                    ".local", "share", "applications"}.Combine ();
 86
 87            string desktopfile_path = Path.Combine (apps_path, "sparkleshare.desktop");
 88
 89            if (!File.Exists (desktopfile_path)) {
 90                if (!Directory.Exists (apps_path))
 91                    Directory.CreateDirectory (apps_path);
 92
 93                TextWriter writer = new StreamWriter (desktopfile_path);
 94                writer.WriteLine ("[Desktop Entry]\n" +
 95                                  "Type=Application\n" +
 96                                  "Name=SparkleShare\n" +
 97                                  "Comment=Share documents\n" +
 98                                  "Exec=sparkleshare start\n" +
 99                                  "Icon=folder-sparkleshare\n" +
100                                  "Terminal=false\n" +
101                                  "Categories=Network;");
102                writer.Close ();
103
104                // Give the launcher the right permissions so it can be launched by the user
105                UnixFileInfo file_info = new UnixFileInfo (desktopfile_path);
106                file_info.FileAccessPermissions = FileAccessPermissions.UserReadWriteExecute;
107
108                SparkleHelpers.DebugInfo ("Controller", "Created '" + desktopfile_path + "'");
109            }
110        }
111
112
113        // Adds the SparkleShare folder to the user's
114        // list of bookmarked places
115        public override void AddToBookmarks ()
116        {
117            string bookmarks_file_path   = Path.Combine (SparkleConfig.DefaultConfig.HomePath, ".gtk-bookmarks");
118            string sparkleshare_bookmark = "file://" + SparkleConfig.DefaultConfig.FoldersPath + " SparkleShare";
119
120            if (File.Exists (bookmarks_file_path)) {
121                StreamReader reader = new StreamReader (bookmarks_file_path);
122                string bookmarks = reader.ReadToEnd ();
123                reader.Close ();
124
125                if (!bookmarks.Contains (sparkleshare_bookmark)) {
126                    TextWriter writer = File.AppendText (bookmarks_file_path);
127                    writer.WriteLine ("file://" + SparkleConfig.DefaultConfig.FoldersPath + " SparkleShare");
128                    writer.Close ();
129                }
130            } else {
131                StreamWriter writer = new StreamWriter (bookmarks_file_path);
132                writer.WriteLine ("file://" + SparkleConfig.DefaultConfig.FoldersPath + " SparkleShare");
133                writer.Close ();
134            }
135        }
136
137
138        // Creates the SparkleShare folder in the user's home folder
139        public override bool CreateSparkleShareFolder ()
140        {
141            if (!Directory.Exists (SparkleConfig.DefaultConfig.FoldersPath)) {
142        
143                Directory.CreateDirectory (SparkleConfig.DefaultConfig.FoldersPath);
144                SparkleHelpers.DebugInfo ("Controller", "Created '" + SparkleConfig.DefaultConfig.FoldersPath + "'");
145
146                string gvfs_command_path =
147                    new string [] {Path.VolumeSeparatorChar.ToString (),
148                        "usr", "bin", "gvfs-set-attribute"}.Combine ();
149
150                // Add a special icon to the SparkleShare folder
151                if (File.Exists (gvfs_command_path)) {
152                    Process process = new Process ();
153
154                    process.StartInfo.RedirectStandardOutput = true;
155                    process.StartInfo.UseShellExecute        = false;
156                    process.StartInfo.FileName               = "gvfs-set-attribute";
157
158                    // Clear the custom (legacy) icon path
159                    process.StartInfo.Arguments = "-t unset " + SparkleConfig.DefaultConfig.FoldersPath + " metadata::custom-icon";
160                    process.Start ();
161                    process.WaitForExit ();
162
163                    // Give the SparkleShare folder an icon name, so that it scales
164                    process.StartInfo.Arguments = SparkleConfig.DefaultConfig.FoldersPath + " metadata::custom-icon-name 'folder-sparkleshare'";
165                    process.Start ();
166                    process.WaitForExit ();
167                }
168
169                return true;
170            }
171
172            return false;
173        }
174        
175
176        public override string EventLogHTML {
177            get {
178                string html_path = new string [] {Defines.PREFIX, "share",
179                    "sparkleshare", "html", "event-log.html"}.Combine ();
180
181                string html = File.ReadAllText (html_path);
182
183                string jquery_file_path = new string [] {Defines.PREFIX, "share",
184                    "sparkleshare", "html", "jquery.js"}.Combine ();
185
186                string jquery = File.ReadAllText (jquery_file_path);
187                html          = html.Replace ("<!-- $jquery -->", jquery);
188            
189                return html;
190            }
191        }
192
193        
194        public override string DayEntryHTML {
195            get {
196                string path = new string [] {Defines.PREFIX,
197                    "share", "sparkleshare", "html", "day-entry.html"}.Combine ();
198            
199                return String.Join (Environment.NewLine, File.ReadAllLines (path));
200            }
201        }
202
203        
204        public override string EventEntryHTML {
205            get {
206                string path = new string [] {Defines.PREFIX,
207                    "share", "sparkleshare", "html", "event-entry.html"}.Combine ();
208            
209                return String.Join (Environment.NewLine, File.ReadAllLines (path));
210            }
211        }
212
213            
214        public override void OpenSparkleShareFolder (string subfolder)
215        {
216            string folder = Path.Combine (SparkleConfig.DefaultConfig.FoldersPath, subfolder);
217
218            Process process = new Process ();
219            process.StartInfo.FileName  = "xdg-open";
220            process.StartInfo.Arguments = "\"" + folder + "\"";
221            process.Start ();
222        }
223
224
225        public override void OpenFile (string url)
226        {
227            Process process = new Process ();
228            process.StartInfo.FileName = "xdg-open";
229            process.StartInfo.Arguments = url.Replace (" ", "%20");
230            process.Start ();
231        }
232    }
233}