rendered paste bodyusing System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using GLib;
using Gdk;
using Gtk;
public class MainWindow: Gtk.Window
{
protected Gtk.EventBox evtMain;
protected Gtk.Image imgMain;
private string config_file;
private string base_directory;
private string album_directory;
private int timeout;
private List<string> files;
private Random random;
private System.Threading.Thread timer;
private object lock_box;
/*~~~ Constructors ~~~*/
public MainWindow (): base ("")
{
Stetic.Gui.Build (this, typeof(MainWindow));
config_file = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
config_file = System.IO.Path.Combine(config_file, ".stacks-frame");
ReadConfig();
files = new List<string>();
random = new Random();
// Set all of the backgrounds to black
ModifyBg(Gtk.StateType.Normal, new Gdk.Color(0, 0, 0));
imgMain.ModifyBg(Gtk.StateType.Normal, new Gdk.Color(0, 0, 0));
evtMain.ModifyBg(Gtk.StateType.Normal, new Gdk.Color(0, 0, 0));
// Hide the cursor
Gdk.Pixmap blank_pixmap = Gdk.Pixmap.CreateFromData(null, "\0", 1, 1, 1, Gdk.Color.Zero, Gdk.Color.Zero);
Gdk.Cursor blank_cursor = new Gdk.Cursor(blank_pixmap, blank_pixmap, Gdk.Color.Zero, Gdk.Color.Zero, 0, 0);
GdkWindow.Cursor = blank_cursor;
// Go full screen and start showing pictures
// Fullscreen();
// StartTimer();
lock_box = new object();
timer = new System.Threading.Thread(new ThreadStart(TimerTick));
timer.Start();
}
/*~~~ Properties ~~~*/
public string Album
{
get { return System.IO.Path.GetFileName(album_directory); }
set
{
lock(lock_box)
{
album_directory = System.IO.Path.Combine(base_directory, value);
files.Clear();
WriteConfig();
}
}
}
public string AlbumDirectory
{
get { return album_directory; }
}
public string BaseDirectory
{
get { return base_directory; }
}
public int Timeout
{
get { return timeout; }
set
{
lock(lock_box)
{
timeout = value;
WriteConfig();
}
}
}
/*~~~ Events ~~~*/
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
protected void OnKeyPressEvent(object o, Gtk.KeyPressEventArgs args)
{
Gdk.EventKey evt_key = args.Event;
if (evt_key.Key == Gdk.Key.Escape)
{
Gtk.Application.Quit();
}
}
/*~~~ Methods ~~~*/
private void ReadConfig ()
{
if (File.Exists(config_file))
{
FileStream stream = new FileStream(config_file, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(stream);
try
{
base_directory = reader.ReadLine();
album_directory = reader.ReadLine();
timeout = Int32.Parse(reader.ReadLine());
}
catch (Exception ex)
{
MessageDialog dialog = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Close, ex.Message);
dialog.Run();
Application.Quit();
}
reader.Close();
stream.Close();
}
else
{
// Setup initial configurations
base_directory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
base_directory = System.IO.Path.Combine(base_directory, "Pictures");
album_directory = System.IO.Path.Combine(base_directory, "Default");
timeout = 60;
// Create the initial album
if (!Directory.Exists(System.IO.Path.Combine(base_directory, album_directory)))
{
Directory.CreateDirectory(album_directory);
}
// Save it
WriteConfig();
}
}
public void RestartTimer()
{
}
private void ShowErrorImage()
{
Gtk.Application.Invoke(delegate { imgMain.SetFromStock(Gtk.Stock.MissingImage, Gtk.IconSize.Dialog); });
}
protected void TimerTick()
{
int i;
string path;
Pixbuf pixbuf;
double screen_width;
double screen_height;
double image_width;
double image_height;
double width_ratio;
double height_ratio;
double scale_ratio;
while (true)
{
Console.WriteLine("Locking...");
lock(lock_box)
{
Console.WriteLine("Locked.");
// Get new pictures or die trying
if (files.Count == 0)
{
Console.WriteLine("Getting files...");
files = new List<string>(Directory.GetFiles(album_directory));
}
if (files.Count == 0)
{
Console.WriteLine("No files to get.");
// We still have no images, so error out
ShowErrorImage();
}
else
{
// Grab a random picture
i = random.Next(files.Count);
path = files[i];
Console.WriteLine("Removing...");
files.RemoveAt(i);
Console.WriteLine("Removed");
// Load the image
try
{
Console.WriteLine("Reading Files...");
pixbuf = new Pixbuf(path);
Console.WriteLine("Read");
screen_width = (double) this.GdkWindow.FrameExtents.Width;
screen_height = (double) this.GdkWindow.FrameExtents.Height;
image_width = (double) pixbuf.Width;
image_height = (double) pixbuf.Height;
width_ratio = screen_width / image_width;
height_ratio = screen_height / image_height;
scale_ratio = Math.Min(width_ratio, height_ratio);
Console.WriteLine("Scaling image...");
pixbuf = pixbuf.ScaleSimple((int)(image_width * scale_ratio), (int)(image_height * scale_ratio), Gdk.InterpType.Hyper);
Console.WriteLine("Setting image...");
Gtk.Application.Invoke(delegate { imgMain.Pixbuf = pixbuf; });
Console.WriteLine("Done.");
}
catch (Exception)
{
Console.WriteLine("Exception");
ShowErrorImage();
}
}
}
Console.WriteLine("Unlocked... Sleeping");
System.Threading.Thread.Sleep(timeout * 1000);
Console.WriteLine("Waking...");
}
}
public void WriteConfig()
{
FileStream stream = new FileStream(config_file, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(base_directory);
writer.WriteLine(album_directory);
writer.WriteLine(timeout);
writer.Close();
stream.Close();
}
}