/*
* Copyright (C) 2006-2007 Eskil Bylund
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Diagnostics;
using Gtk;
using Glade;
using Mono.Unix;
using DCSharp.Backend.Connections;
using DCSharp.Backend.Managers;
using DCSharp.Backend.Objects;
using DCSharp.Extras;
using DCSharp.Xml;
namespace DCSharp.GUI{
public class MainWindow : GladeWindow
{
private IFavoriteHubManager favoriteHubManager;
private PageManager pageManager;
private Page currentPage;
private ActionGroup pageActions;
private uint mergeId;
private UIManager uim;
private ActionGroup actionGroup;
private Action closeAction;
private ToggleAction toolbarVisibleAction;
private Toolbar toolbar;
private MenuToolButton hubToolButton;
private uint timeout;
private static Gdk.Cursor normalCursor = new Gdk.Cursor(Gdk.CursorType.LeftPtr);
private static Gdk.Cursor workingCursor = new Gdk.Cursor(Gdk.CursorType.Watch);
[Widget]
private VBox mainBox;
[Widget]
private Box statusBox;
[Widget]
private Statusbar statusbar;
[Widget]
private ProgressBar progressBar;
#region Constructors
public MainWindow(ConnectionManager connectionManager,
IFavoriteHubManager favoriteHubManager) : base("MainWindow.glade")
{
if (favoriteHubManager == null)
{
throw new ArgumentNullException("favoriteHubManager");
}
this.favoriteHubManager = favoriteHubManager;
Window.Title = DCSharp.Extras.Util.AppName;
ActionEntry[] entries = new ActionEntry[] {
new ActionEntry("HubMenu", null, Catalog.GetString("_Share"), null, null, null),
new ActionEntry("Hubs", Stock.Network, Catalog.GetString("_Hubs"), null,
Catalog.GetString("Edit hubs"),
OnHubs),
new ActionEntry("Search", Stock.Find, Catalog.GetString("_Search..."), "<control>F",
Catalog.GetString("Search for files"),
OnSearch),
new ActionEntry("Preferences", Stock.Preferences, Catalog.GetString("_Preferences"), null,
Catalog.GetString("Edit the preferences"),
OnPreferences),
new ActionEntry("Close", Stock.Close, Catalog.GetString("_Close"), "<control>W",
Catalog.GetString("Close the current tab"),
OnClose),
new ActionEntry("Quit", Stock.Quit, Catalog.GetString("_Quit"), "<control>Q",
Catalog.GetString("Quit the program"),
OnQuit),
// FIXME: Replace "Current" with "Active"/"Pending".
new ActionEntry("ViewMenu", null, Catalog.GetString("_View"), null, null, null),
new ActionEntry("Downloads", Stock.GoDown, Catalog.GetString("_Downloads"), "<control>D",
Catalog.GetString("Current downloads"),
OnDownloads),
new ActionEntry("Uploads", Stock.GoUp, Catalog.GetString("_Uploads"), null,
Catalog.GetString("Current uploads"),
OnUploads),
new ActionEntry("VisibleColumns", null, Catalog.GetString("_Visible Columns..."), null,
Catalog.GetString("Edit visible columns"),
null),
new ActionEntry("TabMenu", null, Catalog.GetString("_Tabs"), null, null, null),
new ActionEntry("HelpMenu", null, Catalog.GetString("_Help"), null, null, null),
new ActionEntry("About", Stock.About, Catalog.GetString("_About"), null,
Catalog.GetString("About this program"),
OnAbout)
};
ToggleActionEntry[] toggleEntries = new ToggleActionEntry[] {
new ToggleActionEntry("ToggleToolbar", null, Catalog.GetString("_Toolbar"), null,
Catalog.GetString("Show or hide the toolbar"),
OnToggleToolbar, true),
new ToggleActionEntry("Transfers", null, Catalog.GetString("_Active Transfers"), null,
Catalog.GetString("Show or hide the active transfers view"),
null, false)
};
actionGroup = new ActionGroup("MainWindowActions");
actionGroup.Add(entries);
actionGroup.Add(toggleEntries);
uim = new UIManager();
uim.AddWidget += OnWidgetAdd;
uim.ConnectProxy += OnProxyConnect;
uim.InsertActionGroup(actionGroup, 0);
uim.AddUiFromResource("MainWindow.ui");
Window.AddAccelGroup(uim.AccelGroup);
closeAction = actionGroup["Close"];
toolbarVisibleAction = (ToggleAction)actionGroup["ToggleToolbar"];
actionGroup["Transfers"].Sensitive = false;
actionGroup["VisibleColumns"].Sensitive = false;
closeAction.Sensitive = false;
// Hub Toolbutton
hubToolButton = new MenuToolButton(Stock.Ok);
actionGroup["Hubs"].ConnectProxy(hubToolButton);
hubToolButton.Menu = favoriteHubManager.HubCount != 0 ? new Menu() : null;
hubToolButton.ShowMenu += delegate
{
UpdateFavoriteHubMenu(hubToolButton);
};
toolbar = (Toolbar)uim.GetWidget("/ToolBar");
toolbar.Insert(hubToolButton, 0);
// Page Manager
pageManager = PageManager.Create();
pageManager.CurrentPageChanged += OnCurrentPageChanged;
pageManager.PageAdded += OnPageAdded;
pageManager.PageRemoved += OnPageRemoved;
pageManager.Show();
mainBox.PackEnd(pageManager, true, true, 0);
// Statusbar
Box connectionStatus = new ConnectionStatusbar(connectionManager);
connectionStatus.Show();
statusBox.PackStart(connectionStatus, false, true, 0);
// Events
favoriteHubManager.HubAdded += OnFavoriteHubCountChanged;
favoriteHubManager.HubRemoved += OnFavoriteHubCountChanged;
}
#endregion
#region Properties
public PageManager PageManager
{
get { return pageManager; }
}
public bool ToolbarVisible
{
get { return toolbarVisibleAction.Active; }
set { toolbarVisibleAction.Active = value; }
}
protected bool IsWorking
{
get { return isWorking; }
set
{
if (value != isWorking)
{
if (value)
{
Window.GdkWindow.Cursor = workingCursor;
if (timeout == 0)
{
progressBar.Visible = true;
timeout = GLib.Timeout.Add(10, OnProgressTimeout);
}
}
else
{
Window.GdkWindow.Cursor = normalCursor;
if (timeout != 0)
{
progressBar.Visible = false;
GLib.Source.Remove(timeout);
timeout = 0;
}
}
isWorking = value;
}
}
}
private bool isWorking;
#endregion
#region Methods
public override void Destroy()
{
favoriteHubManager.HubAdded -= OnFavoriteHubCountChanged;
favoriteHubManager.HubRemoved -= OnFavoriteHubCountChanged;
pageManager.Clear();
base.Destroy();
}
private void UpdateFavoriteHubMenu(MenuToolButton button)
{
button.Menu = new Menu();
foreach (FavoriteHubInfo hubInfo in favoriteHubManager.Hubs)
{
if (hubInfo.Name == null || hubInfo.Name.Length == 0)
{
continue;
}
FavoriteHubInfo hub = hubInfo;
ImageMenuItem item = new ImageMenuItem(hub.Name);
item.Image = new Image(Stock.Network, IconSize.Menu);
if (hub.Hostname == null || hub.Hostname.Length == 0)
{
item.Sensitive = false;
}
item.Activated += delegate
{
HubConnection h = Runtime.ConnectToHub(hub);
GUI.ShowHub(h, true);
};
item.Selected += delegate
{
string text = hub.Description;
if(text != null && text.Length > 0 && hub.Hostname != null)
{
text += " - ";
}
text += hub.Hostname;
statusbar.Push (1, text);
};
item.Deselected += delegate
{
statusbar.Pop (1);
};
((Menu)button.Menu).Append(item);
}
button.Menu.ShowAll();
}
private void UpdateStatus()
{
if (currentPage == null)
{
IsWorking = false;
statusbar.Pop(0);
return;
}
statusbar.Pop(0);
statusbar.Push(0, currentPage.Status != null ?
currentPage.Status : String.Empty);
IsWorking = currentPage.IsWorking;
}
private void OnWidgetAdd (object obj, AddWidgetArgs args)
{
args.Widget.Show();
mainBox.PackStart(args.Widget, false, true, 0);
}
private void OnProxyConnect (object obj, ConnectProxyArgs args)
{
MenuItem item = args.Proxy as MenuItem;
if (item != null)
{
Action action = args.Action;
item.Selected += delegate
{
if (action.Tooltip != null)
{
statusbar.Push (1, action.Tooltip);
}
};
item.Deselected += delegate
{
statusbar.Pop (1);
};
}
}
#region Page manager events
private void OnCurrentPageChanged(object obj, PageEventArgs args)
{
if (currentPage != null)
{
currentPage.StatusChanged -= OnStatusChanged;
}
// Remove the UI from the previous page
if (mergeId != 0)
{
uim.RemoveUi(mergeId);
uim.EnsureUpdate();
mergeId = 0;
}
if (pageActions != null)
{
uim.RemoveActionGroup(pageActions);
pageActions = null;
}
Page page = args.Page;
if (page != null)
{
page.StatusChanged += OnStatusChanged;
// Add the UI for current page
if (page.UI != null)
{
mergeId = uim.AddUiFromString(page.UI);
}
if (page.Actions != null)
{
pageActions = page.Actions;
uim.InsertActionGroup(pageActions, 0);
}
}
currentPage = page;
UpdateStatus();
}
private void OnStatusChanged(object obj, EventArgs args)
{
UpdateStatus();
}
private void OnPageAdded(object obj, PageEventArgs args)
{
closeAction.Sensitive = pageManager.Count > 0;
}
private void OnPageRemoved(object obj, PageEventArgs args)
{
closeAction.Sensitive = pageManager.Count > 0;
}
#endregion
private bool OnProgressTimeout()
{
progressBar.Pulse();
return true;
}
#region Action handlers
private void OnHubs(object obj, EventArgs args)
{
GUI.EditHubs();
}
private void OnSearch(object obj, EventArgs args)
{
GUI.ShowSearch(true);
}
private void OnPreferences(object obj, EventArgs args)
{
GUI.EditPreferences();
}
private void OnClose(object obj, EventArgs args)
{
if (pageManager.Current != null)
{
pageManager.Remove(pageManager.Current);
}
}
private void OnQuit(object obj, EventArgs args)
{
Destroy();
}
private void OnDownloads(object obj, EventArgs args)
{
GUI.ShowDownloads(true);
}
private void OnUploads(object obj, EventArgs args)
{
GUI.ShowUploads(true);
}
private void OnToggleToolbar(object obj, EventArgs args)
{
ToggleAction toggleToolbar = (ToggleAction)obj;
toolbar.Visible = toggleToolbar.Active;
}
private void OnAbout(object obj, EventArgs args)
{
AboutDialog about = new AboutDialog();
about.TransientFor = Window;
about.Run();
about.Destroy();
}
#endregion
private void OnFavoriteHubCountChanged(object obj,
FavoriteHubEventArgs args)
{
Application.Invoke(delegate
{
hubToolButton.Menu = favoriteHubManager.HubCount != 0 ? new Menu() : null;
});
}
#endregion
}
}
|