// Copyright 2005 by Omar Al Zabir. All rights are reserved.
//
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
//
// website http://www.oazabir.com, email OmarAlZabir@gmail.com, msn oazabir@hotmail.com
using System;
using System.IO;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
namespace RSSFeeder{
using RSSCommon;
using RSSCommon.Helper;
using Helpers;
using RSSBlogAPI;
/// <summary>
/// It all starts now...
/// </summary>
public class EntryPoint
{
[STAThread]
public static void Main()
{
// Single instance allowed to run
if( SingleApplication.IsAlreadyRunning() )
{
EntLibHelper.Debug("Main", "Already running" );
SingleApplication.SwitchToCurrentInstance();
return;
}
// Now this line takes too much time, MS needs to make it faster
EntLibHelper.Debug("Main", "Starting..." );
// Hook all unhandled exception, we won't go down without a fight
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
// XP visual style, makes it look cooler
Application.EnableVisualStyles();
Application.DoEvents();
// Before launching, we need to ensure configuration, check if first time run, etc
PrelaunchPreparation();
// Start the app
Application.Run( new RSSServer() );
// Freeup database connection
DatabaseHelper.Close();
EntLibHelper.Debug("Main", "The End" );
}
#region Prelaunch Preparation
/// <summary>
/// Check if the application data directory is created, config files are there, whether
/// this is the first time being launched etc
/// </summary>
private static void PrelaunchPreparation()
{
#region Log
EntLibHelper.Debug("PrelaunchPreparation", "Prelaunch preparation...");
#endregion
// Prepare the path where all application specific settings will be stored
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
ApplicationSettings.ApplicationDataPath = Path.Combine( appDataPath, ApplicationSettings.MY_FOLDER_NAME );
//ApplicationSettings.ConfigurationFilePath = Path.Combine( ApplicationSettings.ApplicationDataPath, ApplicationSettings.CONFIG_FILE_NAME );
ApplicationSettings.DatabaseFilePath = Path.Combine( ApplicationSettings.ApplicationDataPath, ApplicationSettings.DATABASE_NAME );
// Check if all these paths exist
if( !Directory.Exists( ApplicationSettings.ApplicationDataPath ) )
{
#region Log
EntLibHelper.Debug("PrelaunchPreparation", "Creating application data directory: " + ApplicationSettings.ApplicationDataPath );
#endregion
Directory.CreateDirectory( ApplicationSettings.ApplicationDataPath );
}
// Load ocnfiguration
ConfigurationHelper.LoadGlobalSettings();
/// If the database does not exist, then we are running for the first time
if( !File.Exists( ApplicationSettings.DatabaseFilePath ) )
{
CreateDatabase( );
// If user alreay using some other RSS aggregators, let's import their
// setting
if( Helpers.NewsgatorHelper.IsNewsgatorAround() )
{
#region Log
EntLibHelper.Debug("PrelaunchPreparation", "Newsgator detected");
#endregion
using( NewsgatorImport form = new NewsgatorImport() )
{
form.ShowDialog();
}
}
// Show the statup wizard
using( StartupWizard form = new StartupWizard() )
{
#region Log
EntLibHelper.Debug("PrelaunchPreparation", "Launching startup wizard");
#endregion
form.ShowDialog();
}
}
// Copy embedded files
ConfigurationHelper.CopyFiles();
// Perform upgrade work
UpgradeHelper.UpgradeOldVersions();
RegistryHelper.RegisterAtStartup();
}
/// <summary>
/// Create a blank database for storing RSS feed.
/// </summary>
private static void CreateDatabase( )
{
EntLibHelper.Debug("CreateDatabase", "Creating database: " + ApplicationSettings.DatabaseFilePath );
SerializationHelper.WriteEmbeddedFile( "RSSFeeder.mdb", ApplicationSettings.DatabaseFilePath );
}
#endregion
#region Main Form Handling
private static MainForm _MainForm;
/// <summary>
/// Closes the mainform
/// </summary>
public static void CloseMainForm()
{
if( null != _MainForm )
{
_MainForm.Close();
}
}
/// <summary>
/// Show the main form and auto select the specified channel. This function ensures the main
/// form is displayed only once
/// </summary>
/// <param name="channelID"></param>
public static void ShowMainForm(int channelID)
{
if( null != _MainForm )
{
_MainForm.Show();
_MainForm.ShowChannel( channelID );
}
else
{
_MainForm = new MainForm();
_MainForm.Show();
_MainForm.ShowChannel( channelID );
_MainForm.Closed += new EventHandler(_MainForm_Closed);
_MainForm.Disposed += new EventHandler(_MainForm_Disposed);
}
}
/// <summary>
/// When the mainform is disposed, detach from it to release it completely
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void _MainForm_Disposed(object sender, EventArgs e)
{
_MainForm.Disposed -= new EventHandler(_MainForm_Disposed);
_MainForm = null;
MemoryHelper.ReduceMemory();
}
/// <summary>
/// When the main form is closed, dispose it
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void _MainForm_Closed(object sender, EventArgs e)
{
_MainForm.Closed -= new EventHandler(_MainForm_Closed);
if( !_MainForm.Disposing )
_MainForm.Dispose();
}
#endregion
#region Web Log Manager Handling
private static WebLogManager _WebLogManagerForm;
/// <summary>
/// Closes the WebLogManagerForm
/// </summary>
public static void CloseWebLogManagerForm()
{
if( null != _WebLogManagerForm )
{
_WebLogManagerForm.Close();
}
}
/// <summary>
/// Show the main form and auto select the specified channel. This function ensures the main
/// form is displayed only once
/// </summary>
/// <param name="channelID"></param>
public static void ShowWebLogManagerForm()
{
if( null != _WebLogManagerForm )
{
_WebLogManagerForm.Show();
}
else
{
_WebLogManagerForm = new WebLogManager();
_WebLogManagerForm.Show();
try
{
_WebLogManagerForm.SendFeeds += new EventHandler(_WebLogManagerForm_SendFeeds);
_WebLogManagerForm.Closed += new EventHandler(_WebLogManagerForm_Closed);
_WebLogManagerForm.Disposed += new EventHandler(_WebLogManagerForm_Disposed);
}
catch {}
}
}
/// <summary>
/// When the WebLogManagerForm is disposed, detach from it to release it completely
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void _WebLogManagerForm_Disposed(object sender, EventArgs e)
{
_WebLogManagerForm.Disposed -= new EventHandler(_WebLogManagerForm_Disposed);
_WebLogManagerForm = null;
MemoryHelper.ReduceMemory();
}
/// <summary>
/// When the main form is closed, dispose it
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void _WebLogManagerForm_Closed(object sender, EventArgs e)
{
_WebLogManagerForm.SendFeeds -= new EventHandler(_WebLogManagerForm_SendFeeds);
_WebLogManagerForm.Closed -= new EventHandler(_WebLogManagerForm_Closed);
if( !_WebLogManagerForm.Disposing )
_WebLogManagerForm.Dispose();
}
private static void _WebLogManagerForm_SendFeeds(object sender, EventArgs e)
{
RSSHelper.DownloadChannels( _WebLogManagerForm, new ArrayList(), new EventHandler( _WebLogManagerForm.DownloadCompleteEventHandler ), true );
}
#endregion
#region Blogpaper Handling
private static Newspaper _NewspaperForm;
public static void ShowNewspaper( int channelId )
{
if( null != _NewspaperForm )
{
_NewspaperForm.Show();
_NewspaperForm.ShowChannel( channelId );
}
else
{
_NewspaperForm = new Newspaper();
_NewspaperForm.Show();
_NewspaperForm.ShowChannel( channelId );
_NewspaperForm.Closed += new EventHandler(_NewspaperForm_Closed);
_NewspaperForm.Disposed += new EventHandler(_NewspaperForm_Disposed);
}
}
/// <summary>
/// When the mainform is disposed, detach from it to release it completely
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void _NewspaperForm_Disposed(object sender, EventArgs e)
{
_NewspaperForm.Disposed -= new EventHandler(_NewspaperForm_Disposed);
_NewspaperForm = null;
MemoryHelper.ReduceMemory();
}
/// <summary>
/// When the main form is closed, dispose it
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void _NewspaperForm_Closed(object sender, EventArgs e)
{
_NewspaperForm.Closed -= new EventHandler(_NewspaperForm_Closed);
if( !_NewspaperForm.Disposing )
_NewspaperForm.Dispose();
MemoryHelper.ReduceMemory();
}
public static void CloseNewspaper( )
{
if( null != _NewspaperForm )
_NewspaperForm.Close();
}
#endregion
#region Global Exception handling
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
EntLibHelper.UnhandledException(e.Exception);
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if( e.ExceptionObject is Exception )
EntLibHelper.UnhandledException(e.ExceptionObject as Exception);
}
#endregion
}
}
|