CASA/CASA/gui/FileChooser.cs
2007-01-04 08:58:30 +00:00

598 lines
18 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_currentFilter = "*";
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 = "/";
// actions
private int m_iAction = 1;
public const int ACTION_OPEN = 1;
public const int ACTION_SAVE = 2;
public const int ACTION_CHOOSE_DIR = 3;
#region Glade Widgets
[Glade.Widget]
Gtk.Dialog dialogFileChooser,
dialogNewFolder;
[Glade.Widget]
Gtk.TreeView treeviewFavorites,
treeviewListing;
[Glade.Widget]
Gtk.Entry entrySelectedFile,
entryCurrentDir,
entryNewFolder,
entryFilter;
[Glade.Widget]
Gtk.Button buttonUP,
buttonNewFolder;
[Glade.Widget]
Gtk.Menu menuDeleteDirFile;
#endregion
public FileChooser(int action)
{
m_iAction = action;
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 + m_pathSeparator;
//if (m_currentDirectory.EndsWith(m_pathSeparator))
// m_currentDirectory = m_currentDirectory.Substring(0, m_currentDirectory.Length - 1);
SetNewFolderButton();
DisplayDirectory();
}
public string GetFile(string sHintDir, string sHintFile)
{
return GetFile(sHintDir, sHintFile, null);
}
public string GetFile(string sHintDir, string sHintFile, string sFilter)
{
if (sHintDir != null)
{
if (Directory.Exists(sHintDir))
SetCurrentDirectory(sHintDir);
}
if (sHintFile != null)
{
m_hintFile = sHintFile;
}
if (sFilter != null)
{
m_currentFilter = sFilter;
}
DisplayChooser();
if (m_sFileSelected != null)
{
if (m_iAction == FileChooser.ACTION_CHOOSE_DIR)
{
string sDirectory = m_currentDirectory + m_sFileSelected;
if (sDirectory.EndsWith(m_pathSeparator))
{
sDirectory = sDirectory.Substring(0, sDirectory.Length - 1);
}
return sDirectory;
}
else
{
// is there a filter?
if (m_currentFilter.Length > 0)
{
int dotPosition = m_currentFilter.LastIndexOf(".");
if (dotPosition > 0)
{
string sFileExt = m_currentFilter.Substring(dotPosition);
if (!m_sFileSelected.EndsWith(sFileExt) && (!sFileExt.EndsWith("*")))
{
return m_currentDirectory + m_sFileSelected + sFileExt;
}
}
}
}
return m_currentDirectory + m_sFileSelected;
}
else
return null;
}
private void DisplayChooser()
{
// 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);
if (m_iAction == ACTION_CHOOSE_DIR)
{
entrySelectedFile.Visible = false;
dialogFileChooser.Title = "Select a directory";
}
else if (m_iAction == ACTION_OPEN)
{
buttonNewFolder.Visible = false;
entrySelectedFile.Sensitive = false;
dialogFileChooser.Title = "Select the file to open";
}
else
{
dialogFileChooser.Title = "Save secrets to ....";
}
#if LINUX
tsDrives.AppendValues("/home/.casa/"+Common.GetUserName());
tsDrives.AppendValues(Common.GetUserHomeDir());
tsDrives.AppendValues("/media");
#else
// show logical drives
string[] drives = Directory.GetLogicalDrives();
for (int i = 0; i < drives.Length; i++)
{
tsDrives.AppendValues(drives[i]);
}
#endif
SetNewFolderButton();
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);
treeviewListing.MoveCursor += new MoveCursorHandler(treeviewListing_MoveCursor);
treeviewListing.ButtonReleaseEvent += new ButtonReleaseEventHandler(treeviewListing_ButtonReleaseEvent);
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;
if (entryFilter != null)
entryFilter.Text = m_currentFilter;
}
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++)
{
//if (dirs[i].Name.StartsWith(".")) continue;
ts.AppendValues(new Gdk.Pixbuf(Common.IMAGE_PATH + "folder.png"), dirs[i].Name, "", "File folder");
}
if (m_iAction < FileChooser.ACTION_CHOOSE_DIR)
{
FileInfo[] files = dirInfo.GetFiles(m_currentFilter);
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());
}
}
}
public void on_entryFilter_changed(object obj, EventArgs args)
{
m_currentFilter = entryFilter.Text;
DisplayFileListing();
}
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)
{
ProcessSelection();
}
private void ProcessSelection()
{
TreeModel model;
TreeIter iter;
string selected;
// trim any trailing \
//if (m_currentDirectory.EndsWith(m_pathSeparator))
// 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);
selected = entrySelectedFile.Text;
if (Directory.Exists(m_currentDirectory + selected))
{
// if we are at root
if (m_currentDirectory.Equals("/"))
{
SetCurrentDirectory(m_currentDirectory + selected);
}
else
{
SetCurrentDirectory(m_currentDirectory + selected);
}
// clear current view
ts.Clear();
entrySelectedFile.Text = "";
DisplayFileListing();
}
else
{
// file selected
entrySelectedFile.Text = selected;
m_sFileSelected = selected;
// destroy dialog
dialogFileChooser.Destroy();
m_bFileChoosing = false;
}
}
}
catch (Exception e)
{
Logger.DbgLog(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)
{
Logger.DbgLog(e.ToString());
}
}
public void on_btnOkFileChooser_clicked(object obj, EventArgs args)
{
if (m_iAction == ACTION_CHOOSE_DIR)
{
// set directory selected
if ((entrySelectedFile.Text != null) && (entrySelectedFile.Text.Length > 0))
m_sFileSelected = entrySelectedFile.Text + m_pathSeparator;
else
m_sFileSelected = "";
// destroy dialog
dialogFileChooser.Destroy();
m_bFileChoosing = false;
}
else
{
ProcessSelection();
}
}
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)
{
// remove trailing slash if there is one
if (m_currentDirectory.EndsWith(m_pathSeparator))
m_currentDirectory = m_currentDirectory.Substring(0, m_currentDirectory.Length - 1);
// back up one directory
int iSlashIndex = m_currentDirectory.LastIndexOf(m_pathSeparator);
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)
{
}
private void treeviewListing_MoveCursor(object o, MoveCursorArgs args)
{
//Console.WriteLine("Cursor moved");
DisplaySelectedFile();
}
private void treeviewListing_ButtonReleaseEvent(object o, ButtonReleaseEventArgs args)
{
//Console.WriteLine("Button released");
DisplaySelectedFile();
if (3 == args.Event.Button)
{
try
{
Logger.DbgLog("GUI:MiCasa.OnRightClicked() - Context menu opened.");
Glade.XML gxmlTemp = new Glade.XML(Common.GladeFile, "menuDeleteDirFile", null);
gxmlTemp.Autoconnect(this);
menuDeleteDirFile.Popup(null, null, null, IntPtr.Zero, 3, Gtk.Global.CurrentEventTime);
}
catch (Exception exp)
{
Logger.DbgLog("GUI:MiCasa.OnRightClicked() - EXCEPTION:" + exp.ToString());
}
}
}
public void onDeleteDirFile(object obj, EventArgs args)
{
string sDirFile = entrySelectedFile.Text;
if (sDirFile != null)
{
if (Directory.Exists(m_currentDirectory + m_pathSeparator + sDirFile))
{
try
{
Directory.Delete(m_currentDirectory + m_pathSeparator + sDirFile);
}
catch (Exception e)
{
CommonGUI.DisplayMessage(Gtk.MessageType.Error, e.ToString());
}
}
else
{
File.Delete(m_currentDirectory + m_pathSeparator + sDirFile);
}
DisplayFileListing();
}
}
private void DisplaySelectedFile()
{
TreeModel model;
TreeIter iter;
string selected;
try
{
if (treeviewListing.Selection.GetSelected(out model, out iter))
{
selected = (string)model.GetValue(iter, 1);
entrySelectedFile.Text = selected;
}
}
catch (Exception e)
{
}
}
private void SetNewFolderButton()
{
if (buttonNewFolder != null)
{
if ((m_currentDirectory.Equals("/home/.casa/")) ||
(m_currentDirectory.Equals("/media/")))
{
buttonNewFolder.Sensitive = false;
}
else
{
buttonNewFolder.Sensitive = true;
}
}
SetUPButton();
}
private void SetUPButton()
{
if (buttonUP != null)
{
if ((m_currentDirectory.Equals(Common.GetUserHomeDir() + "/")) ||
(m_currentDirectory.Equals("/home/.casa/" + Common.GetUserName() + "/")) ||
(m_currentDirectory.Equals("/media/")))
{
buttonUP.Sensitive = false;
}
else
{
buttonUP.Sensitive = true;
}
}
}
// new folder handler
public void on_buttonNewFolder_clicked(object obj, EventArgs args)
{
//prompt for new folder name
// load and connect handlers
Glade.XML gxmlTemp = new Glade.XML(Common.GladeFile, "dialogNewFolder", null);
gxmlTemp.Autoconnect(this);
dialogNewFolder.SetPosition(Gtk.WindowPosition.CenterOnParent);
}
public void on_okbuttonNewFolder_clicked(object obj, EventArgs args)
{
// create the new folder
string sNewFolder = entryNewFolder.Text;
if (sNewFolder != null)
{
// create folder in currentdir
try
{
Directory.CreateDirectory(m_currentDirectory + m_pathSeparator + sNewFolder);
if (dialogNewFolder != null)
{
dialogNewFolder.Destroy();
}
DisplayFileListing();
}
catch (Exception e)
{
}
}
}
public void on_cancelbuttonNewFolder_clicked(object obj, EventArgs args)
{
if (dialogNewFolder != null)
{
dialogNewFolder.Destroy();
}
}
}
}