main method: fix coding style and whitespace

This commit is contained in:
Hylke Bons 2011-05-02 18:30:34 +01:00
parent e067cc30eb
commit 1a8e7925c4

View file

@ -14,216 +14,173 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using Mono.Unix;
using Mono.Unix.Native;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using SparkleLib;
using SparkleLib.Options;
using System.Text; using System.Text;
using Mono.Unix;
using Mono.Unix.Native;
using SparkleLib;
using SparkleLib.Options;
namespace SparkleShare { namespace SparkleShare {
// This is SparkleShare! // This is SparkleShare!
public class SparkleShare { public class SparkleShare {
public static SparkleController Controller; public static SparkleController Controller;
public static SparkleUI UI; public static SparkleUI UI;
// 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 void Main (string [] args) public static void Main (string [] args)
{ {
// Use translations
// Use translations Catalog.Init (Defines.GETTEXT_PACKAGE, Defines.LOCALE_DIR);
Catalog.Init (Defines.GETTEXT_PACKAGE, Defines.LOCALE_DIR);
// Don't allow running as root
if (new UnixUserInfo (UnixEnvironment.UserName).UserId == 0) {
// Don't allow running as root Console.WriteLine (_("Sorry, you can't run SparkleShare with these permissions."));
if (new UnixUserInfo (UnixEnvironment.UserName).UserId == 0) { Console.WriteLine (_("Things would go utterly wrong."));
Environment.Exit (0);
Console.WriteLine (_("Sorry, you can't run SparkleShare with these permissions.")); }
Console.WriteLine (_("Things would go utterly wrong."));
// Parse the command line options
Environment.Exit (0); bool hide_ui = false;
bool show_help = false;
}
var p = new OptionSet () {
{ "d|disable-gui", _("Don't show the notification icon"), v => hide_ui = v != null },
// Parse the command line options { "v|version", _("Print version information"), v => { PrintVersion (); } },
bool hide_ui = false; { "h|help", _("Show this help text"), v => show_help = v != null }
bool show_help = false; };
var p = new OptionSet () { try {
{ "d|disable-gui", _("Don't show the notification icon"), v => hide_ui = v != null }, p.Parse (args);
{ "v|version", _("Print version information"), v => { PrintVersion (); } }, } catch (OptionException e) {
{ "h|help", _("Show this help text"), v => show_help = v != null } Console.Write ("SparkleShare: ");
}; Console.WriteLine (e.Message);
Console.WriteLine ("Try `sparkleshare --help' for more information.");
try { }
p.Parse (args); if (show_help)
ShowHelp (p);
} catch (OptionException e) {
// Load the right controller for the OS
Console.Write ("SparkleShare: "); string controller_name;
Console.WriteLine (e.Message); switch (SparkleShare.Platform) {
Console.WriteLine ("Try `sparkleshare --help' for more information."); case PlatformID.Unix:
SetProcessName ("sparkleshare");
} controller_name = "Lin";
break;
if (show_help) case PlatformID.MacOSX:
ShowHelp (p); controller_name = "Mac";
break;
case PlatformID.Win32NT:
// Load the right controller for the OS controller_name = "Win";
string controller_name; break;
switch (SparkleShare.Platform) { default:
controller_name = "Lin";
case PlatformID.Unix: break;
SetProcessName ("sparkleshare"); }
controller_name = "Lin";
break; // Initialize the controller this way so that
// there aren't any exceptions in the OS specific UI's
case PlatformID.MacOSX: Controller = (SparkleController) Activator.CreateInstance (
controller_name = "Mac"; Type.GetType ("SparkleShare.Sparkle" + controller_name + "Controller"));
break;
if (Controller != null && !hide_ui) {
case PlatformID.Win32NT: UI = new SparkleUI ();
controller_name = "Win"; UI.Run ();
break; }
}
default:
controller_name = "Lin";
break; // Prints the help output
public static void ShowHelp (OptionSet option_set)
} {
Console.WriteLine (" ");
Console.WriteLine (_("SparkleShare, an instant update workflow to Git."));
// Initialize the controller this way so that Console.WriteLine (_("Copyright (C) 2010 Hylke Bons"));
// there aren't any exceptions in the OS specific UI's Console.WriteLine (" ");
Controller = (SparkleController) Activator.CreateInstance ( Console.WriteLine (_("This program comes with ABSOLUTELY NO WARRANTY."));
Type.GetType ("SparkleShare.Sparkle" + controller_name + "Controller")); Console.WriteLine (" ");
Console.WriteLine (_("This is free software, and you are welcome to redistribute it "));
if (Controller != null && !hide_ui) { Console.WriteLine (_("under certain conditions. Please read the GNU GPLv3 for details."));
Console.WriteLine (" ");
UI = new SparkleUI (); Console.WriteLine (_("SparkleShare automatically syncs Git repositories in "));
UI.Run (); Console.WriteLine (_("the ~/SparkleShare folder with their remote origins."));
Console.WriteLine (" ");
} Console.WriteLine (_("Usage: sparkleshare [start|stop|restart] [OPTION]..."));
Console.WriteLine (_("Sync SparkleShare folder with remote repositories."));
} Console.WriteLine (" ");
Console.WriteLine (_("Arguments:"));
// Prints the help output option_set.WriteOptionDescriptions (Console.Out);
public static void ShowHelp (OptionSet option_set) Environment.Exit (0);
{ }
Console.WriteLine (" ");
Console.WriteLine (_("SparkleShare, an instant update workflow to Git.")); // Prints the version information
Console.WriteLine (_("Copyright (C) 2010 Hylke Bons")); public static void PrintVersion ()
Console.WriteLine (" "); {
Console.WriteLine (_("This program comes with ABSOLUTELY NO WARRANTY.")); Console.WriteLine (_("SparkleShare " + Defines.VERSION));
Console.WriteLine (" "); Environment.Exit (0);
Console.WriteLine (_("This is free software, and you are welcome to redistribute it ")); }
Console.WriteLine (_("under certain conditions. Please read the GNU GPLv3 for details."));
Console.WriteLine (" ");
Console.WriteLine (_("SparkleShare automatically syncs Git repositories in ")); // Strange magic needed by SetProcessName ()
Console.WriteLine (_("the ~/SparkleShare folder with their remote origins.")); [DllImport ("libc")]
Console.WriteLine (" "); private static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
Console.WriteLine (_("Usage: sparkleshare [start|stop|restart] [OPTION]..."));
Console.WriteLine (_("Sync SparkleShare folder with remote repositories."));
Console.WriteLine (" "); // Sets the Unix process name to 'sparkleshare' instead of 'mono'
Console.WriteLine (_("Arguments:")); private static void SetProcessName (string name)
{
option_set.WriteOptionDescriptions (Console.Out); try {
Environment.Exit (0); if (prctl (15, Encoding.ASCII.GetBytes (name + "\0"), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) {
throw new ApplicationException ("Error setting process name: " +
} Mono.Unix.Native.Stdlib.GetLastError ());
}
} catch (EntryPointNotFoundException) {
// Prints the version information Console.WriteLine ("SetProcessName: Entry point not found");
public static void PrintVersion () }
{ }
Console.WriteLine (_("SparkleShare " + Defines.VERSION));
Environment.Exit (0); // Strange magic needed by Platform ()
[DllImport ("libc")]
} static extern int uname (IntPtr buf);
// Strange magic needed by SetProcessName () // This fixes the PlatformID enumeration for MacOSX in Environment.OSVersion.Platform,
[DllImport ("libc")] // which is intentionally broken in Mono for hystorical reasons
private static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5); static PlatformID Platform {
get {
IntPtr buf = IntPtr.Zero;
// Sets the Unix process name to 'sparkleshare' instead of 'mono' try {
private static void SetProcessName (string name) buf = Marshal.AllocHGlobal (8192);
{
if (uname (buf) == 0 && Marshal.PtrToStringAnsi (buf) == "Darwin")
try { return PlatformID.MacOSX;
} catch {
if (prctl (15, Encoding.ASCII.GetBytes (name + "\0"), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) { } finally {
if (buf != IntPtr.Zero)
throw new ApplicationException ("Error setting process name: " + Marshal.FreeHGlobal (buf);
Mono.Unix.Native.Stdlib.GetLastError ()); }
} return Environment.OSVersion.Platform;
}
} catch (EntryPointNotFoundException) { }
}
Console.WriteLine ("SetProcessName: Entry point not found");
}
}
// Strange magic needed by Platform ()
[DllImport ("libc")]
static extern int uname (IntPtr buf);
// This fixes the PlatformID enumeration for MacOSX in Environment.OSVersion.Platform,
// which is intentionally broken in Mono for hystorical reasons
static PlatformID Platform {
get {
IntPtr buf = IntPtr.Zero;
try {
buf = Marshal.AllocHGlobal (8192);
if (uname (buf) == 0 && Marshal.PtrToStringAnsi (buf) == "Darwin")
return PlatformID.MacOSX;
} catch {
} finally {
if (buf != IntPtr.Zero)
Marshal.FreeHGlobal (buf);
}
return Environment.OSVersion.Platform;
}
}
}
} }