2011-07-04 19:44:48 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
|
|
|
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-09-18 18:40:06 +00:00
|
|
|
using System.Threading;
|
2012-05-27 23:14:45 +00:00
|
|
|
|
2011-12-15 15:15:29 +00:00
|
|
|
using SparkleLib;
|
|
|
|
|
2011-07-04 19:44:48 +00:00
|
|
|
namespace SparkleShare {
|
|
|
|
|
|
|
|
public enum IconState {
|
|
|
|
Idle,
|
2011-11-18 21:16:57 +00:00
|
|
|
SyncingUp,
|
|
|
|
SyncingDown,
|
2011-11-28 13:40:34 +00:00
|
|
|
Syncing,
|
2011-07-04 19:44:48 +00:00
|
|
|
Error
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class SparkleStatusIconController {
|
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event UpdateIconEventHandler UpdateIconEvent = delegate { };
|
2012-09-09 12:25:36 +00:00
|
|
|
public delegate void UpdateIconEventHandler (IconState state);
|
2011-07-04 19:44:48 +00:00
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event UpdateMenuEventHandler UpdateMenuEvent = delegate { };
|
2011-07-04 19:44:48 +00:00
|
|
|
public delegate void UpdateMenuEventHandler (IconState state);
|
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event UpdateStatusItemEventHandler UpdateStatusItemEvent = delegate { };
|
2012-03-24 00:13:57 +00:00
|
|
|
public delegate void UpdateStatusItemEventHandler (string state_text);
|
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event UpdateQuitItemEventHandler UpdateQuitItemEvent = delegate { };
|
2012-01-29 22:03:41 +00:00
|
|
|
public delegate void UpdateQuitItemEventHandler (bool quit_item_enabled);
|
|
|
|
|
2012-09-18 18:40:06 +00:00
|
|
|
public event UpdateRecentEventsItemEventHandler UpdateRecentEventsItemEvent = delegate { };
|
|
|
|
public delegate void UpdateRecentEventsItemEventHandler (bool recent_events_item_enabled);
|
2012-04-29 14:19:36 +00:00
|
|
|
|
2011-07-04 19:44:48 +00:00
|
|
|
public IconState CurrentState = IconState.Idle;
|
2012-03-24 00:13:57 +00:00
|
|
|
public string StateText = "Welcome to SparkleShare!";
|
|
|
|
|
2012-07-17 13:09:57 +00:00
|
|
|
public readonly int MenuOverflowThreshold = 9;
|
2012-03-24 20:25:19 +00:00
|
|
|
public readonly int MinSubmenuOverflowCount = 3;
|
2012-03-24 00:13:57 +00:00
|
|
|
|
2012-09-18 18:40:06 +00:00
|
|
|
public string [] Folders;
|
|
|
|
public string [] FolderErrors;
|
|
|
|
|
|
|
|
public string [] OverflowFolders;
|
|
|
|
public string [] OverflowFolderErrors;
|
|
|
|
|
2011-07-04 19:44:48 +00:00
|
|
|
|
|
|
|
public string FolderSize {
|
|
|
|
get {
|
2011-12-15 15:15:29 +00:00
|
|
|
double size = 0;
|
|
|
|
|
2012-03-29 16:05:18 +00:00
|
|
|
foreach (SparkleRepoBase repo in Program.Controller.Repositories)
|
2012-06-05 14:14:58 +00:00
|
|
|
size += repo.Size;
|
2011-12-15 15:15:29 +00:00
|
|
|
|
2012-01-18 23:21:09 +00:00
|
|
|
if (size == 0)
|
|
|
|
return "";
|
|
|
|
else
|
2012-03-24 00:13:57 +00:00
|
|
|
return "— " + Program.Controller.FormatSize (size);
|
2011-07-04 19:44:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-30 00:44:35 +00:00
|
|
|
public int ProgressPercentage {
|
|
|
|
get {
|
|
|
|
return (int) Program.Controller.ProgressPercentage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ProgressSpeed {
|
|
|
|
get {
|
|
|
|
return Program.Controller.ProgressSpeed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 22:03:41 +00:00
|
|
|
public bool QuitItemEnabled {
|
|
|
|
get {
|
2012-07-17 13:09:57 +00:00
|
|
|
return (CurrentState == IconState.Idle || CurrentState == IconState.Error);
|
2012-01-29 22:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 18:40:06 +00:00
|
|
|
public bool RecentEventsItemEnabled {
|
2012-04-29 14:19:36 +00:00
|
|
|
get {
|
2012-07-17 13:09:57 +00:00
|
|
|
return (Program.Controller.RepositoriesLoaded && Program.Controller.Folders.Count > 0);
|
2012-04-29 14:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-04 19:44:48 +00:00
|
|
|
public SparkleStatusIconController ()
|
|
|
|
{
|
2012-09-18 18:40:06 +00:00
|
|
|
UpdateFolders ();
|
|
|
|
|
2011-09-03 20:23:45 +00:00
|
|
|
Program.Controller.FolderListChanged += delegate {
|
2012-03-24 00:13:57 +00:00
|
|
|
if (CurrentState != IconState.Error) {
|
|
|
|
CurrentState = IconState.Idle;
|
|
|
|
|
|
|
|
if (Program.Controller.Folders.Count == 0)
|
|
|
|
StateText = "Welcome to SparkleShare!";
|
|
|
|
else
|
|
|
|
StateText = "Files up to date " + FolderSize;
|
|
|
|
}
|
|
|
|
|
2012-09-18 18:40:06 +00:00
|
|
|
UpdateFolders ();
|
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
UpdateStatusItemEvent (StateText);
|
2012-09-18 18:40:06 +00:00
|
|
|
UpdateRecentEventsItemEvent (RecentEventsItemEnabled);
|
2012-07-18 10:35:22 +00:00
|
|
|
UpdateMenuEvent (CurrentState);
|
2011-07-04 19:44:48 +00:00
|
|
|
};
|
|
|
|
|
2011-09-03 20:23:45 +00:00
|
|
|
Program.Controller.OnIdle += delegate {
|
2012-09-18 18:40:06 +00:00
|
|
|
UpdateFolders ();
|
|
|
|
|
2012-03-24 00:13:57 +00:00
|
|
|
if (CurrentState != IconState.Error) {
|
2011-07-04 19:44:48 +00:00
|
|
|
CurrentState = IconState.Idle;
|
|
|
|
|
2012-03-24 00:13:57 +00:00
|
|
|
if (Program.Controller.Folders.Count == 0)
|
|
|
|
StateText = "Welcome to SparkleShare!";
|
|
|
|
else
|
|
|
|
StateText = "Files up to date " + FolderSize;
|
|
|
|
}
|
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
UpdateQuitItemEvent (QuitItemEnabled);
|
|
|
|
UpdateStatusItemEvent (StateText);
|
2012-09-09 12:25:36 +00:00
|
|
|
UpdateIconEvent (CurrentState);
|
2012-07-18 10:35:22 +00:00
|
|
|
UpdateMenuEvent (CurrentState);
|
2011-07-04 19:44:48 +00:00
|
|
|
};
|
|
|
|
|
2011-09-03 20:23:45 +00:00
|
|
|
Program.Controller.OnSyncing += delegate {
|
2012-03-16 01:23:10 +00:00
|
|
|
int repos_syncing_up = 0;
|
|
|
|
int repos_syncing_down = 0;
|
|
|
|
|
2012-03-29 16:05:18 +00:00
|
|
|
foreach (SparkleRepoBase repo in Program.Controller.Repositories) {
|
2012-03-16 01:23:10 +00:00
|
|
|
if (repo.Status == SyncStatus.SyncUp)
|
|
|
|
repos_syncing_up++;
|
|
|
|
|
|
|
|
if (repo.Status == SyncStatus.SyncDown)
|
|
|
|
repos_syncing_down++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repos_syncing_up > 0 &&
|
|
|
|
repos_syncing_down > 0) {
|
|
|
|
|
|
|
|
CurrentState = IconState.Syncing;
|
2012-09-16 20:49:55 +00:00
|
|
|
StateText = "Syncing changes…";
|
2012-03-16 01:23:10 +00:00
|
|
|
|
|
|
|
} else if (repos_syncing_down == 0) {
|
|
|
|
CurrentState = IconState.SyncingUp;
|
2012-09-16 20:49:55 +00:00
|
|
|
StateText = "Sending changes…";
|
2012-03-16 01:23:10 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
CurrentState = IconState.SyncingDown;
|
2012-09-16 20:49:55 +00:00
|
|
|
StateText = "Receiving changes…";
|
2012-03-16 01:23:10 +00:00
|
|
|
}
|
2011-07-04 19:44:48 +00:00
|
|
|
|
2012-03-24 00:13:57 +00:00
|
|
|
StateText += " " + ProgressPercentage + "% " + ProgressSpeed;
|
|
|
|
|
2012-09-09 12:25:36 +00:00
|
|
|
UpdateIconEvent (CurrentState);
|
2012-07-18 10:35:22 +00:00
|
|
|
UpdateStatusItemEvent (StateText);
|
|
|
|
UpdateQuitItemEvent (QuitItemEnabled);
|
2011-07-04 19:44:48 +00:00
|
|
|
};
|
|
|
|
|
2011-09-03 20:23:45 +00:00
|
|
|
Program.Controller.OnError += delegate {
|
2011-07-04 19:44:48 +00:00
|
|
|
CurrentState = IconState.Error;
|
2012-03-24 00:13:57 +00:00
|
|
|
StateText = "Failed to send some changes";
|
2011-07-04 19:44:48 +00:00
|
|
|
|
2012-09-18 18:40:06 +00:00
|
|
|
UpdateFolders ();
|
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
UpdateQuitItemEvent (QuitItemEnabled);
|
|
|
|
UpdateStatusItemEvent (StateText);
|
2012-09-09 12:25:36 +00:00
|
|
|
UpdateIconEvent (CurrentState);
|
2012-09-18 18:40:06 +00:00
|
|
|
UpdateMenuEvent (CurrentState);
|
2011-07-04 19:44:48 +00:00
|
|
|
};
|
|
|
|
}
|
2012-02-16 23:51:44 +00:00
|
|
|
|
|
|
|
|
2012-02-19 15:00:41 +00:00
|
|
|
public void SparkleShareClicked ()
|
2012-02-16 23:51:44 +00:00
|
|
|
{
|
2012-02-19 15:00:41 +00:00
|
|
|
Program.Controller.OpenSparkleShareFolder ();
|
2012-02-16 23:51:44 +00:00
|
|
|
}
|
2012-02-17 23:06:33 +00:00
|
|
|
|
|
|
|
|
2012-02-19 15:00:41 +00:00
|
|
|
public void SubfolderClicked (string subfolder)
|
2012-02-17 23:06:33 +00:00
|
|
|
{
|
2012-02-19 15:00:41 +00:00
|
|
|
Program.Controller.OpenSparkleShareFolder (subfolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void AddHostedProjectClicked ()
|
|
|
|
{
|
2012-09-26 10:01:58 +00:00
|
|
|
new Thread (() => Program.Controller.ShowSetupWindow (PageType.Add)).Start ();
|
2012-02-17 23:06:33 +00:00
|
|
|
}
|
2012-02-18 00:01:24 +00:00
|
|
|
|
|
|
|
|
2012-09-18 18:40:06 +00:00
|
|
|
public void RecentEventsClicked ()
|
2012-02-18 00:01:24 +00:00
|
|
|
{
|
2012-09-18 18:40:06 +00:00
|
|
|
new Thread (() => Program.Controller.ShowEventLogWindow ()).Start ();
|
2012-02-18 00:01:24 +00:00
|
|
|
}
|
2012-02-19 15:00:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
public void AboutClicked ()
|
|
|
|
{
|
|
|
|
Program.Controller.ShowAboutWindow ();
|
|
|
|
}
|
2012-03-08 14:55:44 +00:00
|
|
|
|
2012-03-16 01:23:10 +00:00
|
|
|
|
2012-03-07 02:26:52 +00:00
|
|
|
public void QuitClicked ()
|
|
|
|
{
|
|
|
|
Program.Controller.Quit ();
|
|
|
|
}
|
2012-09-18 18:40:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
private void UpdateFolders ()
|
|
|
|
{
|
|
|
|
int overflow_count = (Program.Controller.Folders.Count - MenuOverflowThreshold);
|
|
|
|
|
|
|
|
if (overflow_count >= MinSubmenuOverflowCount) {
|
|
|
|
Folders = Program.Controller.Folders.GetRange (0, MenuOverflowThreshold).ToArray ();
|
|
|
|
OverflowFolders = Program.Controller.Folders.GetRange (MenuOverflowThreshold, overflow_count).ToArray ();
|
|
|
|
} else {
|
|
|
|
Folders = Program.Controller.Folders.ToArray ();
|
|
|
|
OverflowFolders = new string [0];
|
|
|
|
}
|
|
|
|
|
|
|
|
string [] errors = new string [Folders.Length];
|
|
|
|
string [] overflow_errors = new string [OverflowFolders.Length];
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
foreach (SparkleRepoBase repo in Program.Controller.Repositories) {
|
|
|
|
string error_message;
|
|
|
|
|
|
|
|
if (repo.Error == ErrorStatus.HostUnreachable) {
|
|
|
|
error_message = "Host unreachable";
|
|
|
|
|
|
|
|
} else if (repo.Error == ErrorStatus.HostIdentityChanged) {
|
|
|
|
error_message = "Host identity changed";
|
|
|
|
|
|
|
|
} else if (repo.Error == ErrorStatus.AuthenticationFailed) {
|
|
|
|
error_message = "Authentication failed";
|
|
|
|
|
|
|
|
} else if (repo.Error == ErrorStatus.DiskSpaceExcedeed) {
|
2012-09-19 18:57:57 +00:00
|
|
|
error_message = "Out of disk space";
|
2012-09-18 18:40:06 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
error_message = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > Folders.Length - 1)
|
|
|
|
overflow_errors [i] = error_message;
|
|
|
|
else
|
|
|
|
errors [i] = error_message;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
FolderErrors = errors;
|
|
|
|
OverflowFolderErrors = overflow_errors;
|
|
|
|
}
|
2011-07-04 19:44:48 +00:00
|
|
|
}
|
|
|
|
}
|