Skip to content

Instantly share code, notes, and snippets.

Created January 5, 2013 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4462285 to your computer and use it in GitHub Desktop.
Save anonymous/4462285 to your computer and use it in GitHub Desktop.
namespace ordoFile
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.Properties["bgStartup"] = false;
if (e != null && e.Args.Count() > 0)
{
if(e.Args[0] == "-bg")
{
this.Properties["bgStartup"] = true;
}
}
Configs configs = new Configs();
PresetFilters presets = new PresetFilters();
OrganisationSyncer organisationSyncer = new OrganisationSyncer();
TrayApp trayApp = new TrayApp(organisationSyncer, configs);
Logger logger = new Logger();
BackgroundOrganiser backgroundOrganiser = new BackgroundOrganiser(new DirectoryModel());
ForegroundOrganiser foregroundOrganiser = new ForegroundOrganiser(new DirectoryModel());
DependencyFactory.Container.RegisterInstance<Configs>("configs", configs);
DependencyFactory.Container.RegisterInstance<PresetFilters>("presets", presets);
DependencyFactory.Container.RegisterInstance<OrganisationSyncer>("organisationSyncer", organisationSyncer);
DependencyFactory.Container.RegisterInstance<BackgroundOrganiser>("backgroundOrganiser", backgroundOrganiser);
DependencyFactory.Container.RegisterInstance<ForegroundOrganiser>("foregroundOrganiser", foregroundOrganiser);
DependencyFactory.Container.RegisterInstance<TrayApp>("trayApp", trayApp);
DependencyFactory.Container.RegisterInstance<Logger>("logger", logger);
MainViewModel mainViewModel = new MainViewModel(trayApp, organisationSyncer);
MainView mainView = new MainView(configs, trayApp, mainViewModel);
mainView.Show();
}
}
}
<Window x:Class="ordoFile.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ordoFile.ViewModels"
xmlns:vw="clr-namespace:ordoFile.Views"
Title="ordoFile"
Name="MainWindow"
Height="500"
Width="815">
<Window.Style>
<Style TargetType="Window">
<Setter Property="Visibility" Value="{Binding WindowVisibility}" />
</Style>
</Window.Style>
<Grid Name="ContentGrid">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<vw:ForegroundOrganiserView Grid.RowSpan="2"/>
<vw:BackgroundOrganiserView Grid.Row="2"/>
</Grid>
</Window>
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Security;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using ordoFile.Models;
using ordoFile.DataAccess;
using ordoFile.GUITools;
using ordoFile.Models.Organisers;
using ordoFile.ViewModels;
namespace ordoFile.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainView : Window
{
Configs _configs;
TrayApp _trayApp;
public MainView(Configs configs, TrayApp trayApp, MainViewModel viewModel)
{
_configs = configs;
_trayApp = trayApp;
this.DataContext = viewModel;
InitializeComponent();
}
/// <summary>
/// Override OnClosing to hide window rather than exiting application
/// when MainViews close button has been clicked. Application exiting
/// has been deferred to the TrayApp.
/// </summary>
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if (_trayApp.WindowShouldMinimise)
{
e.Cancel = true;
_trayApp.ChangeGUIVisibility();
}
else
{
_configs.SaveConfigs();
}
base.OnClosing(e);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ordoFile
{
/// <summary>
/// Class for ensuring foreground and background organisers do not
/// organise the same directory at the same time. Also synchronises
/// MainViews visibility state with actions in TrayApp.
/// </summary>
public class OrganisationSyncer
{
// Background/foreground organiser running states used
// for ensuring the same folder is not organised by both
// organisers
bool _backgroundOrganiserRunning,
_foregroundOrganiserRunning;
// Background/foreground organiser root directories used
// for ensuring the same folder is not organised by both
// organisers
string _backgroundDirectory,
_foregroundDirectory;
System.Windows.Visibility _windowVisibility = System.Windows.Visibility.Hidden;
/// <summary>
/// Get or set the background directory path.
/// </summary>
public string BackgroundDirectory
{
get { return _backgroundDirectory; }
set { _backgroundDirectory = value; }
}
/// <summary>
/// Get or set the foreground directory path.
/// </summary>
public string ForegroundDirectory
{
get { return _foregroundDirectory; }
set { _foregroundDirectory = value; }
}
/// <summary>
/// Get or set the state of the background organiser
/// </summary>
public bool BackgroundOrganiserRunning
{
get { return _backgroundOrganiserRunning; }
set { _backgroundOrganiserRunning = value; }
}
/// <summary>
/// Get or set the state of the foreground organiser
/// </summary>
public bool ForegroundOrganiserRunning
{
get { return _foregroundOrganiserRunning; }
set { _foregroundOrganiserRunning = value; }
}
/// <summary>
/// Get or set the state of the foreground organiser
/// </summary>
public System.Windows.Visibility WindowVisibilty
{
get { return _windowVisibility; }
set { _windowVisibility = value; }
}
/// <summary>
/// Handler for the event to be run when background organisation
/// is to be started.
/// </summary>
public event EventHandler OrganiseInBackground;
/// <summary>
/// Handler for the event to be run when background organisation
/// is to be stopped.
/// </summary>
public event EventHandler StopBackgroundOrganisation;
/// <summary>
/// Method which, when called, raises the event for running
/// background organisation or the event for cancelling it
/// </summary>
/// <param name="shouldOrganiseInBackground">
/// Boolean value which decides whether or not start/stop events
/// are raised for background organisation
/// </param>
public void ShouldOrganiseInBackground(bool shouldOrganiseInBackground)
{
if (shouldOrganiseInBackground)
{
if (OrganiseInBackground != null)
{
OrganiseInBackground(this, EventArgs.Empty);
}
}
else
{
if (StopBackgroundOrganisation != null)
{
StopBackgroundOrganisation(this, EventArgs.Empty);
}
}
}
/// <summary>
/// Handler for the event to be run when background organisation
/// is to be stopped.
/// </summary>
public event EventHandler ChangeOrganisationTextInTray;
/// <summary>
/// Method which, when called, raises the event for running
/// changing the organisation state text in the tray icon.
/// </summary>
public void ChangeOrganisationText()
{
if (ChangeOrganisationTextInTray != null)
{
ChangeOrganisationTextInTray(this, EventArgs.Empty);
}
}
/// <summary>
/// Handler for the event to be run when window visibility should be changed.
/// </summary>
public event EventHandler UpdateWindowVisibility;
/// <summary>
/// Method which, when called, raises the event for updating
/// the main window's visibility
/// </summary>
public void UpdateVisibility()
{
if (UpdateWindowVisibility != null)
{
UpdateWindowVisibility(this, EventArgs.Empty);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ordoFile.DataAccess;
using ordoFile.Models.Organisers;
namespace ordoFile
{
public partial class TrayApp : Form
{
// Icon for tray app
NotifyIcon _trayIcon = new NotifyIcon();
//Menu for tray app
ContextMenu _trayMenu = new ContextMenu();
// Menu items to be placed in context menu
MenuItem _showGUI, _bgState, _exit;
// Reading and saving of config settings
Configs _configs;
// Ensures background/foreground organiser do not organise
// the same folder; also syncs view states between GUI and
// tray app
OrganisationSyncer _organisationSyncer;
// Represent states for whether or not the MainWindow should
// be hidden, and whether or not it close button should minimise
// window or exit application
bool _hideWindow,
_windowShouldMinimise;
public TrayApp(OrganisationSyncer organisationSyncer, Configs configs)
{
_organisationSyncer = organisationSyncer;
_configs = configs;
OnInitialise();
InitializeComponent();
}
/// <summary>
/// Returns wether or the window should be minimised
/// when MainWindow's close button is clicked
/// </summary>
public bool WindowShouldMinimise
{
get { return _windowShouldMinimise; }
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Hide form window.
this.Visible = false;
this.ShowInTaskbar = false;
}
void OnInitialise()
{
// Pass the event used for changing the _bgState display text
// to organisation syncer, which will be raised on input
// in the GUI
_organisationSyncer.ChangeOrganisationTextInTray += ChangeOrganisationText;
// Set the application to minimise when close button
// of main window is clicked
_windowShouldMinimise = true;
// Set whether or not the GUI should be hidden on startup
_hideWindow = ((bool) System.Windows.Application.Current.Properties["bgStartup"]);
if (_hideWindow)
{
_organisationSyncer.WindowVisibilty = System.Windows.Visibility.Hidden;
_organisationSyncer.UpdateVisibility();
}
// Instantiate tray menu, and menu items to be added to it
_showGUI = new MenuItem();
_showGUI.Text = (_hideWindow) ? "Show Window" : "Hide Window";
_showGUI.Click += (a, e) => { ChangeGUIVisibility(); };
_bgState = new MenuItem();
_bgState.Text = (_organisationSyncer.BackgroundOrganiserRunning) ? "Stop Organising" : "Start Organising";
_bgState.Enabled = _configs.BGDirectoryExists;
_bgState.Click += (a, e) => { CheckOrganisationState(); };
_exit = new MenuItem();
_exit.Text = "Exit";
_exit.Click += (a, e) => { OnExit(); };
_trayMenu.MenuItems.Add(0, _showGUI);
_trayMenu.MenuItems.Add(1, _bgState);
_trayMenu.MenuItems.Add(2, _exit);
_trayIcon.Icon = new Icon("icon.ico", 38, 42);
_trayIcon.Text = "ordoFile";
_trayIcon.DoubleClick += (a, b) => { ChangeGUIVisibility(); };
_trayIcon.ContextMenu = _trayMenu;
_trayIcon.Visible = true;
}
/// <summary>
/// Method which sets visibility of GUI and tray menu text
/// </summary>
public void ChangeGUIVisibility()
{
if (_organisationSyncer.WindowVisibilty == System.Windows.Visibility.Visible)
{
_showGUI.Text = "Show window";
_organisationSyncer.WindowVisibilty = System.Windows.Visibility.Hidden;
_organisationSyncer.UpdateVisibility();
}
else
{
_showGUI.Text = "Hide window";
_organisationSyncer.WindowVisibilty = System.Windows.Visibility.Visible;
_organisationSyncer.UpdateVisibility();
}
}
/// <summary>
/// Check organisation current organisation state, start or
/// stop organisation depending on whether or not background
/// directory exists then update menu item with appropriate text
/// </summary>
void CheckOrganisationState()
{
if (!_organisationSyncer.BackgroundOrganiserRunning)
{
if (_configs.BGDirectoryExists)
{
_bgState.Text = "Stop Organising";
_organisationSyncer.ShouldOrganiseInBackground(true);
}
}
else
{
_bgState.Text = "Start Organising";
_organisationSyncer.ShouldOrganiseInBackground(false);
}
}
/// <summary>
/// Event to update organisation state text in context menu
/// depending on current organisation state.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ChangeOrganisationText(object sender, EventArgs e)
{
if (_organisationSyncer.BackgroundOrganiserRunning)
{
_bgState.Text = "Stop Organising";
}
else
{
_bgState.Text = "Start Organising";
}
}
/// <summary>
/// Checks to run on exit of application.
/// </summary>
void OnExit()
{
_organisationSyncer.ShouldOrganiseInBackground(false);
_configs.SaveConfigs();
_windowShouldMinimise = false;
System.Windows.Application.Current.Shutdown();
this.Close();
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
// Release the icon resource.
_trayIcon.Dispose();
}
if (isDisposing && (components != null))
{
components.Dispose();
}
base.Dispose(isDisposing);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment