jamin on December 20th, 2002

I’ve been playing around with C# and Mono lately. I’ve written my first (nominally) useful app. It’s a simple application to mark myself away. Basically I can use it to set an away message in a central place. Then, any application with a concept of “awayness” can (using a plugin), read that away status and respond. So my IRC client, Instant messenger, etc. all respond to the away status. It also allows me to select if I want mail to be forwarded to my mobile device or not. It looks like this. This is a very customized application and I doubt it’ll be useful to anyone other than me, however I’ll share the code because I can. :)

Click below…

I won’t bother including the glade file, if you really want it, contact me. Here’s the C# code:


// GoAway.cs
//
// Author: Jamin P. Gray
//
// (c) 2002 Jamin P. Gray
using System;
using System.IO;
using System.Environment;
using System.Diagnostics;
using Gtk;
using Gdk;
using Glade;
using Gnome;
using GLib;
using GtkSharp;
public class GoAway : Gnome.Program
{
    // Members
    private Gtk.Button myButton;
    private Gtk.Entry myEntry;
    private Gtk.CheckButton myCheckButton;
    private Gnome.AppBar myAppBar;
    private bool away;
    private string message;
    private string file;
    private const string VERSION = "0.01";
    public static void Main (string[] args)
    {
        GoAway app = new GoAway(args);
        app.Run();
    }
    public GoAway(string[] args)
        : base ("GoAway", VERSION, Modules.UI, args)
    {
        Glade.XML appGxml = new Glade.XML (null, "goaway.glade", "app", null);
        appGxml.Autoconnect (this);
        // Import the widgets we'll need to work with directly
        myButton = (Gtk.Button) appGxml["button"];
        myEntry = (Gtk.Entry) appGxml["entry"];
        myCheckButton = (Gtk.CheckButton) appGxml["checkbutton"];
        myAppBar = (Gnome.AppBar) appGxml["appbar"];
        // Get our file name
        file = System.Environment.GetEnvironmentVariable("HOME") + "/.away";
        // Set our away status to false
        SetAwayStatus(false);
    }
    public void OnWindowDeleteEvent (object o, EventArgs args)
    {
        this.Quit();
    }
    public void OnAboutActivateEvent (object o, EventArgs args)
    {
        string [] authors = new string [] {
            "Jamin Philip Gray (jamin@pubcrawler.org)",
        };
        string [] documenters = new string [] {
            "Jamin Philip Gray (jamin@pubcrawler.org)",
        };
        Gdk.Pixbuf pixbuf = new Gdk.Pixbuf ("gnome.png");
        Gnome.About about = new Gnome.About ("GoAway", VERSION,
                             "Copyright (C) 2002, Jamin Philip Gray",
                             "A small application to mark oneself away",
                             authors, documenters, null,
                             pixbuf);
        about.Show ();
    }
    public void OnCheckButtonToggledEvent (object o, EventArgs args)
    {
        string cmd;
        if (myCheckButton.Active)
        {
            cmd = "away";
        }
        else
        {
            cmd = "bak";
        }
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo(cmd);
        p.Start();
    }
    public void OnChangeStatusEvent (System.Object obj, EventArgs e)
    {
        SetAwayStatus (!away);
    }
    public void SetAwayStatus (bool away)
    {
        this.away = away;
        myEntry.Editable = !away;
        string msg = myEntry.Text;

        if (away)
        {
            myButton.Label = "I'm Back!";
            myAppBar.SetStatus ("Status: Away");
        }
        else
        {
            myButton.Label = "Go Away!";
            myEntry.Text = "";
            myAppBar.SetStatus ("Status: Present");
            msg = "";
        }
        try
        {
            FileInfo f = new FileInfo(file);
            StreamWriter w = f.CreateText();
            w.WriteLine(away);
            w.WriteLine(msg);
            w.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Can't write to file: {0}", file);
            Console.WriteLine(e.Message);
        }
    }
}

Tags: