298 lines
7.2 KiB
C#
298 lines
7.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Threading;
|
|
|
|
using Gtk;
|
|
|
|
namespace Novell.CASA.GUI
|
|
{
|
|
/// <summary>
|
|
/// Summary description for FileChooser.
|
|
/// </summary>
|
|
public class FileChooser
|
|
{
|
|
private string m_currentDirectory;
|
|
private string m_hintFile = null;
|
|
private string m_sFileSelected = null;
|
|
private bool m_bFileChoosing = true;
|
|
|
|
private Gtk.TreeStore tsDrives = new TreeStore(typeof(string));
|
|
private Gtk.TreeStore ts = new TreeStore(typeof(Gdk.Pixbuf), typeof(string), typeof(string), typeof(string));
|
|
|
|
private string m_pathSeparator = "/";
|
|
|
|
Thread tChooserThread = null;
|
|
|
|
|
|
#region Glade Widgets
|
|
|
|
[Glade.Widget]
|
|
Gtk.Dialog dialogFileChooser;
|
|
|
|
[Glade.Widget]
|
|
Gtk.TreeView treeviewFavorites,
|
|
treeviewListing;
|
|
|
|
[Glade.Widget]
|
|
Gtk.Entry entrySelectedFile,
|
|
entryCurrentDir;
|
|
|
|
[Glade.Widget]
|
|
Gtk.Button buttonUP;
|
|
|
|
|
|
#endregion
|
|
|
|
public FileChooser()
|
|
{
|
|
|
|
if (Common.IS_WINDOWS)
|
|
m_pathSeparator = "\\";
|
|
|
|
SetCurrentDirectory(Common.GetUserHomeDir());
|
|
}
|
|
|
|
private void SetCurrentDirectory(string dir)
|
|
{
|
|
// replace all instances of the path separator with correct one
|
|
if (Common.IS_WINDOWS)
|
|
m_currentDirectory = dir.Replace("/", "\\");
|
|
else
|
|
m_currentDirectory = dir;
|
|
|
|
|
|
//if (m_currentDirectory.EndsWith(m_pathSeparator))
|
|
// m_currentDirectory = m_currentDirectory.Substring(0, m_currentDirectory.Length - 1);
|
|
|
|
DisplayDirectory();
|
|
}
|
|
|
|
public string GetFile(string sHintDir, string sHintFile)
|
|
{
|
|
if (sHintDir != null)
|
|
{
|
|
if (Directory.Exists(sHintDir))
|
|
SetCurrentDirectory(sHintDir);
|
|
}
|
|
|
|
if (sHintFile != null)
|
|
{
|
|
m_hintFile = sHintFile;
|
|
}
|
|
|
|
DoWork();
|
|
return m_currentDirectory + m_pathSeparator + m_sFileSelected;
|
|
|
|
}
|
|
|
|
private void DoWork()
|
|
{
|
|
|
|
// display chooser
|
|
Show();
|
|
|
|
// wait for user
|
|
while (m_bFileChoosing)
|
|
{
|
|
// Flush pending events to keep the GUI reponsive
|
|
while (Gtk.Application.EventsPending ())
|
|
Gtk.Application.RunIteration ();
|
|
Thread.Sleep(100);
|
|
}
|
|
}
|
|
|
|
public void Show(string sHintDir, string sHintFile)
|
|
{
|
|
|
|
if (sHintDir != null)
|
|
{
|
|
if (Directory.Exists(sHintDir))
|
|
SetCurrentDirectory(sHintDir);
|
|
}
|
|
|
|
if (sHintFile != null)
|
|
{
|
|
m_hintFile = sHintFile;
|
|
}
|
|
|
|
this.Show();
|
|
}
|
|
|
|
public void Show()
|
|
{
|
|
// load and connect handlers
|
|
Glade.XML gxmlTemp = new Glade.XML(Common.GladeFile, "dialogFileChooser", null);
|
|
gxmlTemp.Autoconnect(this);
|
|
|
|
// show logical drives
|
|
string[] drives = Directory.GetLogicalDrives();
|
|
for (int i=0; i<drives.Length; i++)
|
|
{
|
|
tsDrives.AppendValues(drives[i]);
|
|
}
|
|
treeviewFavorites.AppendColumn("Drive", new CellRendererText(), "text", 0);
|
|
treeviewFavorites.Model = tsDrives;
|
|
|
|
treeviewFavorites.RowActivated += new RowActivatedHandler(treeviewFavorites_RowActivated);
|
|
|
|
// FILE LISTING
|
|
// hook up the model
|
|
TreeViewColumn col1 = new TreeViewColumn("", new Gtk.CellRendererPixbuf(), "pixbuf", 0);
|
|
treeviewListing.AppendColumn(col1);
|
|
treeviewListing.AppendColumn("Name",new CellRendererText(),"text",1);
|
|
treeviewListing.AppendColumn("Size",new CellRendererText(),"text",2);
|
|
treeviewListing.AppendColumn("Date Modified",new CellRendererText(),"text",3);
|
|
treeviewListing.Model = ts;
|
|
|
|
// hook up the events
|
|
treeviewListing.RowActivated +=new RowActivatedHandler(treeviewListing_RowActivated);
|
|
|
|
DisplayDirectory();
|
|
DisplayFileListing();
|
|
|
|
if (m_hintFile != null)
|
|
entrySelectedFile.Text = m_hintFile;
|
|
}
|
|
|
|
public string GetSelectedFile()
|
|
{
|
|
return m_sFileSelected;
|
|
}
|
|
|
|
private void DisplayDirectory()
|
|
{
|
|
if (entryCurrentDir != null)
|
|
entryCurrentDir.Text = m_currentDirectory;
|
|
}
|
|
|
|
private void DisplayFileListing()
|
|
{
|
|
ts.Clear();
|
|
|
|
// add in the dirs and files
|
|
DirectoryInfo dirInfo = new DirectoryInfo(m_currentDirectory);
|
|
DirectoryInfo[] dirs = dirInfo.GetDirectories();
|
|
|
|
for (int i=0; i<dirs.Length; i++)
|
|
{
|
|
ts.AppendValues(new Gdk.Pixbuf (Common.IMAGE_PATH + "folder.png", 16, 16), dirs[i].Name, "", "File folder");
|
|
}
|
|
|
|
FileInfo[] files = dirInfo.GetFiles();
|
|
for (int i=0; i<files.Length; i++)
|
|
{
|
|
ts.AppendValues(new Gdk.Pixbuf (Common.IMAGE_PATH + "CASA_16.png"), files[i].Name, (files[i].Length/1000).ToString() + " KB", files[i].LastWriteTime.ToShortDateString() + " " + files[i].LastWriteTime.ToShortTimeString());
|
|
}
|
|
}
|
|
|
|
private int GetItemCount(string sDir)
|
|
{
|
|
DirectoryInfo dirInfo = new DirectoryInfo(sDir);
|
|
DirectoryInfo[] dirs = dirInfo.GetDirectories();
|
|
FileInfo[] files = dirInfo.GetFiles();
|
|
return dirs.Length + files.Length;
|
|
}
|
|
|
|
private void treeviewListing_RowActivated(object o, RowActivatedArgs args)
|
|
{
|
|
TreeModel model;
|
|
TreeIter iter;
|
|
string selected;
|
|
|
|
// trim any trailing \
|
|
if (m_currentDirectory.EndsWith("\\"))
|
|
m_currentDirectory = m_currentDirectory.Substring(0, m_currentDirectory.Length - 1);
|
|
|
|
|
|
try
|
|
{
|
|
if( treeviewListing.Selection.GetSelected (out model, out iter) )
|
|
{
|
|
selected = (string) model.GetValue(iter, 1);
|
|
|
|
if (Directory.Exists(m_currentDirectory + "/" + selected))
|
|
{
|
|
SetCurrentDirectory(m_currentDirectory + "/" + selected);
|
|
|
|
// clear current view
|
|
ts.Clear();
|
|
DisplayFileListing();
|
|
}
|
|
else
|
|
{
|
|
// file selected
|
|
entrySelectedFile.Text = selected;
|
|
m_sFileSelected = selected;
|
|
// destroy dialog
|
|
dialogFileChooser.Destroy();
|
|
m_bFileChoosing = false;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
}
|
|
|
|
}
|
|
|
|
private void treeviewFavorites_RowActivated(object o, RowActivatedArgs args)
|
|
{
|
|
// change drive
|
|
TreeModel model;
|
|
TreeIter iter;
|
|
string selected;
|
|
|
|
try
|
|
{
|
|
if( treeviewFavorites.Selection.GetSelected (out model, out iter) )
|
|
{
|
|
selected = (string)model.GetValue(iter, 0);
|
|
SetCurrentDirectory(selected);
|
|
DisplayFileListing();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
}
|
|
}
|
|
|
|
public void on_btnCancelFileChooser_clicked(object obj, EventArgs args)
|
|
{
|
|
m_bFileChoosing = false;
|
|
if (dialogFileChooser != null)
|
|
{
|
|
dialogFileChooser.Destroy();
|
|
}
|
|
}
|
|
|
|
|
|
public void on_dialogFileChooser_destroy(object o, EventArgs args)
|
|
{
|
|
m_bFileChoosing = false;
|
|
}
|
|
|
|
public void on_buttonUP_clicked(object o, EventArgs args)
|
|
{
|
|
int iSlashIndex = m_currentDirectory.LastIndexOf(m_pathSeparator);
|
|
// back up one directory
|
|
if (iSlashIndex > 1)
|
|
{
|
|
// if windows drive letter, keep the slash
|
|
if ((Common.IS_WINDOWS) && (iSlashIndex == 2))
|
|
SetCurrentDirectory(m_currentDirectory.Substring(0, iSlashIndex + 1));
|
|
else
|
|
SetCurrentDirectory(m_currentDirectory.Substring(0, iSlashIndex));
|
|
|
|
DisplayFileListing();
|
|
}
|
|
}
|
|
|
|
private void imageUp_ButtonPressEvent(object o, ButtonPressEventArgs args)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|