Code cleanup

This commit is contained in:
Hylke Bons 2010-07-24 15:03:58 +01:00
parent 2d2aa11a75
commit 0b20eca93f
3 changed files with 131 additions and 146 deletions

View file

@ -25,7 +25,6 @@ using System.Timers;
namespace SparkleShare { namespace SparkleShare {
// Holds repository information and timers
public class SparkleRepo public class SparkleRepo
{ {
@ -45,34 +44,25 @@ namespace SparkleShare {
public string UserEmail; public string UserEmail;
public string UserName; public string UserName;
public delegate void AddedEventHandler (object o, SparkleEventArgs args); public delegate void AddedEventHandler (object o, SparkleEventArgs args);
public event AddedEventHandler Added;
public delegate void CommitedEventHandler (object o, SparkleEventArgs args); public delegate void CommitedEventHandler (object o, SparkleEventArgs args);
public event CommitedEventHandler Commited;
public delegate void PushingStartedEventHandler (object o, SparkleEventArgs args); public delegate void PushingStartedEventHandler (object o, SparkleEventArgs args);
public event PushingStartedEventHandler PushingStarted;
public delegate void PushingFinishedEventHandler (object o, SparkleEventArgs args); public delegate void PushingFinishedEventHandler (object o, SparkleEventArgs args);
public event PushingFinishedEventHandler PushingFinished;
public delegate void FetchingStartedEventHandler (object o, SparkleEventArgs args); public delegate void FetchingStartedEventHandler (object o, SparkleEventArgs args);
public event FetchingStartedEventHandler FetchingStarted;
public delegate void FetchingFinishedEventHandler (object o, SparkleEventArgs args); public delegate void FetchingFinishedEventHandler (object o, SparkleEventArgs args);
public event FetchingFinishedEventHandler FetchingFinished;
public delegate void NewCommitEventHandler (object o, NewCommitArgs args); public delegate void NewCommitEventHandler (object o, NewCommitArgs args);
public delegate void ConflictDetectedEventHandler (object o, SparkleEventArgs args);
public event AddedEventHandler Added;
public event CommitedEventHandler Commited;
public event PushingStartedEventHandler PushingStarted;
public event PushingFinishedEventHandler PushingFinished;
public event FetchingStartedEventHandler FetchingStarted;
public event FetchingFinishedEventHandler FetchingFinished;
public event NewCommitEventHandler NewCommit; public event NewCommitEventHandler NewCommit;
public event ConflictDetectedEventHandler ConflictDetected;
public static string _ (string s)
{
return Catalog.GetString (s);
}
public SparkleRepo (string path) public SparkleRepo (string path)
{ {
@ -177,8 +167,10 @@ namespace SparkleShare {
FetchTimer.Stop (); FetchTimer.Stop ();
lock (ChangeLock) { lock (ChangeLock) {
LastChange = DateTime.UtcNow; LastChange = DateTime.UtcNow;
HasChanged = true; HasChanged = true;
} }
} }
@ -197,13 +189,15 @@ namespace SparkleShare {
FetchTimer.Stop (); FetchTimer.Stop ();
Add (); Add ();
string message = FormatCommitMessage (); string message = FormatCommitMessage ();
if (!message.Equals ("")) { if (!message.Equals ("")) {
Commit (message); Commit (message);
Fetch (); Fetch ();
Push (); Push ();
CheckForUnicorns (message);
} }
} finally { } finally {
@ -237,16 +231,17 @@ namespace SparkleShare {
// Commits the made changes // Commits the made changes
public void Commit (string Message) public void Commit (string message)
{ {
SparkleHelpers.DebugInfo ("Commit", "[" + Name + "] " + Message); SparkleHelpers.DebugInfo ("Commit", "[" + Name + "] " + message);
Process.StartInfo.Arguments = "commit -m \"" + Message + "\""; Process.StartInfo.Arguments = "commit -m \"" + message + "\"";
Process.Start (); Process.Start ();
Process.WaitForExit (); Process.WaitForExit ();
SparkleEventArgs args = new SparkleEventArgs ("Commited"); SparkleEventArgs args = new SparkleEventArgs ("Commited");
args.Message = message;
if (Commited != null) if (Commited != null)
Commited (this, args); Commited (this, args);
@ -335,23 +330,18 @@ namespace SparkleShare {
Process.WaitForExit (); Process.WaitForExit ();
Process.Start (); Process.Start ();
string TimeStamp = DateTime.Now.ToString ("H:mm d MMM yyyy"); string timestamp = DateTime.Now.ToString ("H:mm d MMM yyyy");
File.Move (problem_file_name, File.Move (problem_file_name, problem_file_name + " (" + UserName + ", " + timestamp + ")");
problem_file_name + " (" + UserName + ", " + TimeStamp + ")");
Process.StartInfo.Arguments Process.StartInfo.Arguments = "checkout --theirs " + problem_file_name;
= "checkout --theirs " + problem_file_name;
Process.WaitForExit (); Process.WaitForExit ();
Process.Start (); Process.Start ();
string conflict_title = "A mid-air collision happened!\n"; SparkleEventArgs args = new SparkleEventArgs ("ConflictDetected");
string conflict_subtext = "Don't worry, SparkleShare made\na copy of each conflicting file.";
// SparkleBubble ConflictBubble = if (ConflictDetected != null)
// new SparkleBubble(_(ConflictTitle), _(ConflictSubtext)); ConflictDetected (this, args);
// ConflictBubble.Show ();
} }
@ -385,10 +375,10 @@ namespace SparkleShare {
Process.Start (); Process.Start ();
string message = Process.StandardOutput.ReadToEnd ().Trim (); string message = Process.StandardOutput.ReadToEnd ().Trim ();
NewCommitArgs args = new NewCommitArgs (author, email, message); NewCommitArgs new_commit_args = new NewCommitArgs (author, email, message);
if (NewCommit != null) if (NewCommit != null)
NewCommit (this, args); NewCommit (this, new_commit_args);
} }
@ -649,32 +639,17 @@ namespace SparkleShare {
} }
// Checks for unicorns
public static void CheckForUnicorns (string s) {
s = s.ToLower ();
if (s.Contains ("unicorn") && (s.Contains (".png") || s.Contains (".jpg"))) {
string title = _("Hold your ponies!");
string subtext = _("SparkleShare is known to be insanely fast with \n" +
"pictures of unicorns. Please make sure your internets\n" +
"are upgraded to the latest version to avoid problems.");
// SparkleBubble unicorn_bubble = new SparkleBubble (title, subtext);
// unicorn_bubble.Show ();
}
}
} }
public class SparkleEventArgs : System.EventArgs { public class SparkleEventArgs : System.EventArgs {
public string Type;
public string Message; public string Message;
public SparkleEventArgs (string s) public SparkleEventArgs (string type)
{ {
Message = s; Type = type;
} }
} }

View file

@ -27,40 +27,48 @@ namespace SparkleShare {
public class SparkleUI { public class SparkleUI {
private Process Process; public static SparkleStatusIcon NotificationIcon;
public static List <SparkleRepo> Repositories; public static List <SparkleRepo> Repositories;
private Process Process;
// Short alias for the translations // Short alias for the translations
public static string _(string s) public static string _(string s)
{ {
return Catalog.GetString (s); return Catalog.GetString (s);
} }
public static SparkleStatusIcon NotificationIcon;
public SparkleUI (bool HideUI) public SparkleUI (bool HideUI)
{ {
Repositories = new List <SparkleRepo> (); Repositories = new List <SparkleRepo> ();
Process = new Process (); Process = new Process () {
Process.EnableRaisingEvents = true; EnableRaisingEvents = true
};
Process.StartInfo.RedirectStandardOutput = true; Process.StartInfo.RedirectStandardOutput = true;
Process.StartInfo.UseShellExecute = false; Process.StartInfo.UseShellExecute = false;
string SparklePath = SparklePaths.SparklePath;
EnableSystemAutostart (); EnableSystemAutostart ();
InstallLauncher (); InstallLauncher ();
// Create the SparkleShare folder and add it to the bookmarks
if (!Directory.Exists (SparklePaths.SparklePath)) {
CreateSparkleShareFolder (); CreateSparkleShareFolder ();
AddToBookmarks ();
}
// Create a directory to store temporary files in // Create a directory to store temporary files in
if (!Directory.Exists (SparklePaths.SparkleTmpPath)) if (!Directory.Exists (SparklePaths.SparkleTmpPath))
Directory.CreateDirectory (SparklePaths.SparkleTmpPath); Directory.CreateDirectory (SparklePaths.SparkleTmpPath);
CreateConfigurationFolders ();
UpdateRepositories (); UpdateRepositories ();
// Don't create the window and status // Don't create the window and status
// icon when --disable-gui was given // icon when --disable-gui was given
if (!HideUI) { if (!HideUI) {
@ -79,53 +87,27 @@ namespace SparkleShare {
} }
// TODO: This crashes
/*
// Watch the SparkleShare folder and pop up the
// Add dialog when a new folder is created
FileSystemWatcher Watcher = new FileSystemWatcher (SparklePaths.SparklePath);
Watcher.IncludeSubdirectories = false;
Watcher.EnableRaisingEvents = true;
Watcher.Created += delegate (object o, FileSystemEventArgs args) {
WatcherChangeTypes wct = args.ChangeType;
SparkleHelpers.DebugInfo ("Event",
wct.ToString () +
" '" + args.Name + "'");
SparkleDialog SparkleDialog = new SparkleDialog ();
SparkleDialog.ShowAll ();
};
// When a repo folder is deleted, don't sync and update the UI
Watcher.Deleted += delegate (object o, FileSystemEventArgs args) {
WatcherChangeTypes wct = args.ChangeType;
SparkleHelpers.DebugInfo ("Event",
wct.ToString () +
" '" + args.Name + "'");
SparkleUI SparkleUI = new SparkleUI ();
SparkleUI.ShowAll ();
};
*/
// Create place to store configuration user's home folder
string ConfigPath = SparklePaths.SparkleConfigPath;
string LocalIconPath = SparklePaths.SparkleLocalIconPath;
if (!Directory.Exists (ConfigPath)) {
Directory.CreateDirectory (ConfigPath);
SparkleHelpers.DebugInfo ("Config", "Created '" + ConfigPath + "'");
// Create a place to store the avatars
Directory.CreateDirectory (LocalIconPath);
SparkleHelpers.DebugInfo ("Config", "Created '" + LocalIconPath + "'");
} }
string notify_setting_file = SparkleHelpers.CombineMore (SparklePaths.SparkleConfigPath,
"sparkleshare.notify"); // Creates a folder in the user's home folder to store configuration
public void CreateConfigurationFolders ()
{
string config_path = SparklePaths.SparkleConfigPath;
string local_icon_path = SparklePaths.SparkleLocalIconPath;
if (!Directory.Exists (config_path)) {
// Create a folder to store settings
Directory.CreateDirectory (config_path);
SparkleHelpers.DebugInfo ("Config", "Created '" + config_path + "'");
// Create a folder to store the avatars
Directory.CreateDirectory (local_icon_path);
SparkleHelpers.DebugInfo ("Config", "Created '" + local_icon_path + "'");
string notify_setting_file = SparkleHelpers.CombineMore (config_path, "sparkleshare.notify");
// Enable notifications by default // Enable notifications by default
if (!File.Exists (notify_setting_file)) if (!File.Exists (notify_setting_file))
@ -133,6 +115,8 @@ namespace SparkleShare {
} }
}
// Creates .desktop entry in autostart folder to // Creates .desktop entry in autostart folder to
// start SparkleShare automnatically at login // start SparkleShare automnatically at login
@ -168,6 +152,8 @@ namespace SparkleShare {
} }
// Installs a launcher so the user can launch SparkleShare
// from the Internet category if needed
public void InstallLauncher () public void InstallLauncher ()
{ {
@ -205,20 +191,13 @@ namespace SparkleShare {
public void AddToBookmarks () public void AddToBookmarks ()
{ {
// Add the SparkleShare folder to the bookmarks
switch (SparklePlatform.Name) {
case "GNOME":
string bookmarks_file_name = Path.Combine (SparklePaths.HomePath, ".gtk-bookmarks"); string bookmarks_file_name = Path.Combine (SparklePaths.HomePath, ".gtk-bookmarks");
if (File.Exists (bookmarks_file_name)) { if (File.Exists (bookmarks_file_name)) {
TextWriter writer = File.AppendText (bookmarks_file_name); TextWriter writer = File.AppendText (bookmarks_file_name);
writer.WriteLine ("file://" + SparklePaths.SparklePath + " SparkleShare"); writer.WriteLine ("file://" + SparklePaths.SparklePath + " SparkleShare");
writer.Close (); writer.Close ();
}
break;
} }
@ -230,33 +209,21 @@ namespace SparkleShare {
public void CreateSparkleShareFolder () public void CreateSparkleShareFolder ()
{ {
if (!Directory.Exists (SparklePaths.SparklePath)) {
Directory.CreateDirectory (SparklePaths.SparklePath); Directory.CreateDirectory (SparklePaths.SparklePath);
SparkleHelpers.DebugInfo ("Config", "Created '" + SparklePaths.SparklePath + "'"); SparkleHelpers.DebugInfo ("Config", "Created '" + SparklePaths.SparklePath + "'");
// Add a special icon to the SparkleShare folder // Add a special icon to the SparkleShare folder
switch (SparklePlatform.Name) {
case "GNOME":
Process.StartInfo.FileName = "gvfs-set-attribute"; Process.StartInfo.FileName = "gvfs-set-attribute";
Process.StartInfo.Arguments = SparklePaths.SparklePath + " metadata::custom-icon " + Process.StartInfo.Arguments = SparklePaths.SparklePath + " metadata::custom-icon " +
"file:///usr/share/icons/hicolor/48x48/places/" + "file:///usr/share/icons/hicolor/48x48/places/" +
"folder-sparkleshare.png"; "folder-sparkleshare.png";
Process.Start (); Process.Start ();
break;
}
AddToBookmarks ();
}
} }
// Shows a notification bubble when someone
// made a change to the repository
public void ShowNewCommitBubble (string author, string email, string message) { public void ShowNewCommitBubble (string author, string email, string message) {
string notify_settings_file = SparkleHelpers.CombineMore (SparklePaths.SparkleConfigPath, string notify_settings_file = SparkleHelpers.CombineMore (SparklePaths.SparkleConfigPath,
@ -273,6 +240,20 @@ namespace SparkleShare {
} }
// Shows a notification bubble when there
// was a conflict
public void ShowConflictBubble (object o, EventArgs args) {
string title = _("Ouch! Mid-air collision!");
string subtext = _("Don't worry, SparkleShare made a copy of each conflicting file.");
SparkleBubble bubble = new SparkleBubble(title, subtext);
bubble.Show ();
}
// Updates the statusicon to the syncing state
public void UpdateStatusIconSyncing (object o, EventArgs args) public void UpdateStatusIconSyncing (object o, EventArgs args)
{ {
@ -282,6 +263,7 @@ namespace SparkleShare {
} }
// Updates the syncing icon to the idle state
public void UpdateStatusIconIdle (object o, EventArgs args) public void UpdateStatusIconIdle (object o, EventArgs args)
{ {
@ -309,6 +291,10 @@ namespace SparkleShare {
Application.Invoke (delegate { ShowNewCommitBubble (args.Author, args.Email, args.Message); }); Application.Invoke (delegate { ShowNewCommitBubble (args.Author, args.Email, args.Message); });
}; };
repo.Commited += delegate (object o, SparkleEventArgs args) {
Application.Invoke (delegate { CheckForUnicorns (args.Message); });
};
repo.FetchingStarted += delegate { repo.FetchingStarted += delegate {
Application.Invoke (UpdateStatusIconSyncing); Application.Invoke (UpdateStatusIconSyncing);
}; };
@ -325,6 +311,10 @@ namespace SparkleShare {
Application.Invoke (UpdateStatusIconIdle); Application.Invoke (UpdateStatusIconIdle);
}; };
repo.ConflictDetected += delegate {
Application.Invoke (ShowConflictBubble);
};
Repositories.Add (repo); Repositories.Add (repo);
} }
@ -333,6 +323,26 @@ namespace SparkleShare {
} }
// Warns the user implicitly that unicorns are actually lethal creatures
public static void CheckForUnicorns (string message) {
message = message.ToLower ();
if (message.Contains ("unicorn") && (message.Contains (".png") || message.Contains (".jpg"))) {
string title = _("Hold your ponies!");
string subtext = _("SparkleShare is known to be insanely fast with " +
"pictures of unicorns. Please make sure your internets " +
"are upgraded to the latest version to avoid any problems.");
SparkleBubble bubble = new SparkleBubble (title, subtext);
bubble.Show ();
}
}
} }
} }

View file

@ -42,7 +42,7 @@ namespace SparkleShare {
{ {
SparkleRepo = sparkle_repo; SparkleRepo = sparkle_repo;
SetSizeRequest (550, 720); SetSizeRequest (540, 640);
SetPosition (WindowPosition.Center); SetPosition (WindowPosition.Center);
BorderWidth = 12; BorderWidth = 12;
@ -219,7 +219,7 @@ namespace SparkleShare {
layout_vertical.PackStart (date_label, false, false, 0); layout_vertical.PackStart (date_label, false, false, 0);
IconView icon_view = new IconView (list_store) { IconView icon_view = new IconView (list_store) {
ItemWidth = 480, ItemWidth = 470,
MarkupColumn = 1, MarkupColumn = 1,
Orientation = Orientation.Horizontal, Orientation = Orientation.Horizontal,
PixbufColumn = 0, PixbufColumn = 0,