Source

gnome-record-screen / GNOME-Record-Screen / src / UI / SaveDialog.cs

/**
 * SaveDIalog.cs
 *
 * :copyright: 2008 mitsuhiko.
 * :license: BSD
 */

using System;
using System.Collections.Generic;
using System.IO;
using Gtk;

using Gnome.RecordScreen;
using Gnome.RecordScreen.UI;

namespace Gnome.RecordScreen {
	
	public class SaveDialog : Gtk.FileChooserDialog {
		private ScreenRecorder recorder;
		
		public SaveDialog (MainWindow mainWindow)
			: base ("Save record", mainWindow,
			        FileChooserAction.Save,
			        Gtk.Stock.Cancel, ResponseType.Cancel,
			        Gtk.Stock.Save, ResponseType.Accept)
		{
			recorder = mainWindow.Recorder;
			VideoEncoder enc = recorder.VideoEncoder;
			
			// the filter for the selected container
			FileFilter filter = new FileFilter ();
			foreach (string ext in enc.Container.Extensions)
				filter.AddPattern ("*" + ext);
			filter.Name = recorder.VideoEncoder.Name;
			AddFilter (filter);
			Filter = filter;
			
			// and for all files
			filter = new FileFilter ();
			filter.AddPattern ("*");
			filter.Name = "All files";
			AddFilter (filter);
			
			string filename;
			if (MultiFileMode)
				filename = "recorded-desktop-###";
			else {
				DoOverwriteConfirmation = true;
				filename = Utils.AddDateToFilename ("Recorded Desktop");
			}			
				
			CurrentName = filename + enc.Container.Extensions[0];
		}
		
		public bool MultiFileMode {
			get { return recorder.VideoEncoder.Container.RequiresMultiFileSink; }
		}
		
		public void MoveRecordedFiles ()
		{			
			if (MultiFileMode) {
				string pattern = Filename;
				if (pattern.IndexOf ('#') < 0)
					pattern = Utils.AddToFilename (pattern, "#");
				int index = 0;
				foreach (string src in recorder.CreatedTemporaryFiles) {
					if (!File.Exists (src))
						continue;
					string dst = Utils.AddFormatedNumber (pattern, index++);
					if (File.Exists (dst))
						File.Delete (dst);
					File.Move (src, dst);
				}
			}
			else {
				if (File.Exists (Filename))
					File.Delete (Filename);
				File.Move (recorder.TemporaryFilename, Filename);
			}
		}
		
		public void DeleteRecordedFiles ()
		{
			recorder.DeleteTemporaryFiles ();
		}
		
		public void MoveOrDelete ()
		{
			if ((ResponseType)Run() == ResponseType.Accept) {
				MoveRecordedFiles ();
			}
			else
				DeleteRecordedFiles ();
			Destroy ();
		}
	}
}