linux: Refactor autostart

This commit is contained in:
Hylke Bons 2016-03-31 19:30:58 +01:00
parent 996f2b2ce7
commit 08844e26d6
6 changed files with 135 additions and 147 deletions

View file

@ -32,43 +32,42 @@ namespace SparkleShare {
{
}
public override bool CreateSparkleShareFolder ()
{
CreateStartupItem ();
public override string PresetsPath {
get {
return Path.Combine (InstallationInfo.Directory, "presets");
}
if (Directory.Exists (Configuration.DefaultConfig.FoldersPath))
return false;
Directory.CreateDirectory (Configuration.DefaultConfig.FoldersPath);
Syscall.chmod (Configuration.DefaultConfig.FoldersPath, (FilePermissions) 448); // 448 -> 700
return false;
}
public override void SetFolderIcon ()
{
var command = new Command ("gvfs-set-attribute", Configuration.DefaultConfig.FoldersPath + " " +
"metadata::custom-icon-name org.sparkleshare.SparkleShare");
command.StartAndWaitForExit ();
}
// Creates a .desktop entry in autostart folder to
// start SparkleShare automatically at login
public override void CreateStartupItem ()
{
string autostart_path = Path.Combine (Config.HomePath, ".config", "autostart");
string autostart_file_path = Path.Combine (autostart_path, "org.sparkleshare.SparkleShare.Autostart.desktop");
string autostart_file_path = Path.Combine (Path.GetDirectoryName (InstallationInfo.Directory),
"applications", "SparkleShare.Autostart.desktop");
string autostart_file_dest = Path.Combine (Config.HomePath, ".config", "autostart", "SparkleShare.Autostart.desktop");
string autostart_path = Path.GetDirectoryName (autostart_file_dest);
if (File.Exists (autostart_file_path))
return;
if (!Directory.Exists (autostart_path))
if (!Directory.Exists (autostart_path))
Directory.CreateDirectory (autostart_path);
string autostart_exec = "sparkleshare";
if (InstallationInfo.Directory.StartsWith ("/app/"))
autostart_exec = "xdg-app run org.sparkleshare.SparkleShare";
// TODO: Ship as .desktop file and copy in place
try {
File.WriteAllText (autostart_file_path,
"[Desktop Entry]\n" +
"Name=SparkleShare\n" +
"Type=Application\n" +
"Exec=" + autostart_exec + "\n" +
"Icon=org.sparkleshare.SparkleShare\n" +
"Terminal=false\n" +
"X-GNOME-Autostart-enabled=true\n");
File.Copy (autostart_file_path, autostart_file_dest);
Logger.LogInfo ("Controller", "Added SparkleShare to startup items");
} catch (Exception e) {
@ -77,24 +76,33 @@ namespace SparkleShare {
}
// Creates the SparkleShare folder in the user's home folder
public override bool CreateSparkleShareFolder ()
public override void InstallProtocolHandler ()
{
if (!Directory.Exists (Configuration.DefaultConfig.FoldersPath)) {
Directory.CreateDirectory (Configuration.DefaultConfig.FoldersPath);
Syscall.chmod (Configuration.DefaultConfig.FoldersPath, (FilePermissions) 448); // 448 -> 700
return true;
}
return false;
}
public override void CopyToClipboard (string text)
{
Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false)).Text = text;
}
public override void OpenFolder (string path)
{
OpenFile (path);
}
public override void OpenFile (string path)
{
new Command ("xdg-open", "\"" + path + "\"").Start ();
}
public override string EventLogHTML {
get {
string html_path = Path.Combine (InstallationInfo.Directory, "html", "event-log.html");
string jquery_file_path = Path.Combine (InstallationInfo.Directory, "html", "jquery.js");
string jquery_file_path = Path.Combine (InstallationInfo.Directory, "html", "jquery.js");
string html = File.ReadAllText (html_path);
string jquery = File.ReadAllText (jquery_file_path);
@ -120,36 +128,10 @@ namespace SparkleShare {
}
public override void CopyToClipboard (string text)
{
Clipboard clipboard = Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));
clipboard.Text = text;
}
public override void SetFolderIcon ()
{
var process = new Command ("gvfs-set-attribute", Configuration.DefaultConfig.FoldersPath + " " +
"metadata::custom-icon-name org.sparkleshare.SparkleShare");
process.StartAndWaitForExit ();
}
public override void OpenFolder (string path)
{
OpenFile (path);
}
public override void OpenFile (string path)
{
new Command ("xdg-open", "\"" + path + "\"").Start ();
}
public override void InstallProtocolHandler ()
{
public override string PresetsPath {
get {
return Path.Combine (InstallationInfo.Directory, "presets");
}
}
}
}

View file

@ -38,7 +38,8 @@ bin_SCRIPTS = sparkleshare
Applicationsdir = $(datadir)/applications
dist_Applications_DATA = \
org.sparkleshare.SparkleShare.desktop \
org.sparkleshare.SparkleShare.Invites.desktop
org.sparkleshare.SparkleShare.Invites.desktop \
SparkleShare.Autostart.desktop
install-data-hook:
test -f $(datadir)/applications/defaults.list && \

View file

@ -0,0 +1,8 @@
[Desktop Entry]
Name=SparkleShare
Type=Application
Exec=sh -c \"if type "sparkleshare" > /dev/null; then sparkleshare else xdg-app run org.sparkleshare.SparkleShare fi"
Icon=org.sparkleshare.SparkleShare
Terminal=false
X-GNOME-Autostart-enabled=true

View file

@ -5,5 +5,4 @@ Exec=sparkleshare
Icon=org.sparkleshare.SparkleShare
Terminal=false
Categories=Network;FileTransfer;GNOME;GTK;
X-GNOME-Autostart-enabled=true

View file

@ -58,71 +58,6 @@ namespace SparkleShare {
}
// We have to use our own custom made folder watcher, as
// System.IO.FileSystemWatcher fails watching subfolders on Mac
SparkleMacWatcher watcher;
delegate void FileActivityTask ();
FileActivityTask MacActivityTask (BaseRepository repo, FileSystemEventArgs fse_args)
{
return delegate { new Thread (() => { repo.OnFileActivity (fse_args); }).Start (); };
}
void OnFilesChanged (List<string> changed_files_in_basedir)
{
var triggered_repos = new List<string> ();
foreach (string file in changed_files_in_basedir) {
string repo_name;
int path_sep_index = file.IndexOf (Path.DirectorySeparatorChar);
if (path_sep_index >= 0)
repo_name = file.Substring (0, path_sep_index);
else
repo_name = file;
repo_name = Path.GetFileNameWithoutExtension (repo_name);
BaseRepository repo = GetRepoByName (repo_name);
if (repo == null)
continue;
if (!triggered_repos.Contains (repo_name)) {
triggered_repos.Add (repo_name);
FileActivityTask task = MacActivityTask (repo,
new FileSystemEventArgs (WatcherChangeTypes.Changed, file, "Unknown"));
task ();
}
}
}
public override void CreateStartupItem ()
{
// There aren't any bindings in MonoMac to support this yet, so
// we call out to an applescript to do the job
string args = "-e 'tell application \"System Events\" to " +
"make login item at end with properties " +
"{path:\"" + NSBundle.MainBundle.BundlePath + "\", hidden:false}'";
var process = new Command ("osascript", args);
process.StartAndWaitForExit ();
Logger.LogInfo ("Controller", "Added " + NSBundle.MainBundle.BundlePath + " to login items");
}
public override void InstallProtocolHandler ()
{
// We ship SparkleShareInviteHandler.app in the bundle
}
public override bool CreateSparkleShareFolder ()
{
if (!Directory.Exists (SparkleShare.Controller.FoldersPath)) {
@ -151,6 +86,34 @@ namespace SparkleShare {
}
public override void CreateStartupItem ()
{
// There aren't any bindings in MonoMac to support this yet, so
// we call out to an applescript to do the job
string args = "-e 'tell application \"System Events\" to " +
"make login item at end with properties " +
"{path:\"" + NSBundle.MainBundle.BundlePath + "\", hidden:false}'";
var process = new Command ("osascript", args);
process.StartAndWaitForExit ();
Logger.LogInfo ("Controller", "Added " + NSBundle.MainBundle.BundlePath + " to login items");
}
public override void InstallProtocolHandler ()
{
}
public override void CopyToClipboard (string text)
{
NSPasteboard.GeneralPasteboard.ClearContents ();
NSPasteboard.GeneralPasteboard.SetStringForType (text, "NSStringPboardType");
}
public override void OpenFolder (string path)
{
path = Uri.UnescapeDataString (path);
@ -171,13 +134,6 @@ namespace SparkleShare {
}
public override void CopyToClipboard (string text)
{
NSPasteboard.GeneralPasteboard.ClearContents ();
NSPasteboard.GeneralPasteboard.SetStringForType (text, "NSStringPboardType");
}
string event_log_html;
public override string EventLogHTML
{
@ -223,6 +179,48 @@ namespace SparkleShare {
}
SparkleMacWatcher watcher;
delegate void FileActivityTask ();
// We have to use our own custom made folder watcher, as
// System.IO.FileSystemWatcher fails watching subfolders on Mac
FileActivityTask MacActivityTask (BaseRepository repo, FileSystemEventArgs fse_args)
{
return delegate { new Thread (() => { repo.OnFileActivity (fse_args); }).Start (); };
}
void OnFilesChanged (List<string> changed_files_in_basedir)
{
var triggered_repos = new List<string> ();
foreach (string file in changed_files_in_basedir) {
string repo_name;
int path_sep_index = file.IndexOf (Path.DirectorySeparatorChar);
if (path_sep_index >= 0)
repo_name = file.Substring (0, path_sep_index);
else
repo_name = file;
repo_name = Path.GetFileNameWithoutExtension (repo_name);
BaseRepository repo = GetRepoByName (repo_name);
if (repo == null)
continue;
if (!triggered_repos.Contains (repo_name)) {
triggered_repos.Add (repo_name);
FileActivityTask task = MacActivityTask (repo,
new FileSystemEventArgs (WatcherChangeTypes.Changed, file, "Unknown"));
task ();
}
}
}
public delegate void Code ();
readonly NSObject obj = new NSObject ();

View file

@ -22,7 +22,7 @@ namespace Sparkles {
public static class ListenerFactory {
private static List<BaseListener> listeners = new List<BaseListener> ();
static readonly List<BaseListener> listeners = new List<BaseListener> ();
public static BaseListener CreateListener (string folder_name, string folder_identifier)
@ -40,9 +40,9 @@ namespace Sparkles {
// Please see the SparkleShare wiki if you wish to run
// your own service instead
if (string.IsNullOrEmpty (uri))
uri = "tcp://notifications.sparkleshare.org:443";
uri = "tcp://announcements.sparkleshare.org:443";
Uri announce_uri = new Uri (uri);
var announce_uri = new Uri (uri);
// Use only one listener per notification service to keep
// the number of connections as low as possible
@ -53,14 +53,14 @@ namespace Sparkles {
// We already seem to have a listener for this server,
// refer to the existing one instead
listener.AlsoListenTo (folder_identifier);
return (BaseListener) listener;
return listener;
}
}
listeners.Add (new TcpListener (announce_uri, folder_identifier));
Logger.LogInfo ("ListenerFactory", "Issued new listener for " + announce_uri);
return (BaseListener) listeners [listeners.Count - 1];
return listeners [listeners.Count - 1];
}
}
}