//
// Copyright 2014 Perforce Software Inc.
//
using Perforce.Helper;
using Perforce.Model;
using Perforce.ViewModel;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Technewlogic.WpfDialogManagement;
namespace Perforce.View {
///
/// Interaction logic for ToolBar.xaml
///
public partial class ToolBar : UserControl {
private ToolBarViewModel _model;
public ToolBar() {
InitializeComponent();
_model = new ToolBarViewModel();
this.DataContext = _model;
}
public ToolBarViewModel Model {
get { return _model; }
set { _model = value; }
}
public void Refresh() {
_model.Refresh();
}
private void SyncButton_Click(object sender, RoutedEventArgs e) {
var main = App.Current.MainWindow as MainWindow;
var selector = UIHelper.GetCurrentSelector();
var helper = Utility.GetPerforceHelper();
var dialog = main.DialogManager.CreateWaitDialog("synchronizing workspace with server", DialogMode.Ok);
dialog.VerticalDialogAlignment = System.Windows.VerticalAlignment.Top;
dialog.CanOk = false;
dialog.Show(() => {
Utility.StopBackgroundProcesses();
var list = helper.RunSync("//...", notifyOnError: true);
if (list != null && list.Count > 0) {
Log.Debug(string.Format("{0} files synchronized from server", list.Count));
var flist = new List();
foreach (var smd in list)
{
flist.Add(PerforceHelper.UnescapePath(smd.DepotPath.Path));
}
var mdList = helper.GetFileMetaData(flist);
if (mdList != null)
{
foreach (var md in mdList) {
var item = new FileItem();
item.Metadata = md;
item.LabelText = md.GetFileName();
if (md.HeadAction != P4.FileAction.Delete && md.HeadAction != P4.FileAction.MoveDelete) {
Utility.GetRecentSet().Add(item);
}
}
}
UIHelper.RefreshSelectorAsync(SELECTOR_TYPE.RECENT);
} else {
Log.Debug("sync: no files downloaded");
}
if (selector != null) {
UIHelper.RefreshSelectorAsync(selector.Model.SelectorType);
}
Utility.StartBackgroundProcesses();
dialog.CanOk = true;
if (list != null && list.Count > 0) {
dialog.Message = string.Format("sync complete: {0} changes synchronized from server", list.Count);
} else {
dialog.Message = "sync complete: no files downloaded";
}
Thread.Sleep(20000);
});
}
private void CheckInButton_Click(object sender, RoutedEventArgs e) {
CommandHelper.SubmitChangelist();
}
private void SearchTextbox_KeyUp(object sender, KeyEventArgs e) {
if (e.Key.Equals(Key.Return) && SearchTextbox.Text.Trim().Length > 0) {
var searchStr = SearchTextbox.Text.Trim();
// if the search string starts with the p4:// prefix, we just open to the path
if (searchStr.StartsWith("p4://")) {
UIHelper.GoToPath(searchStr);
} else {
// the resetSearchModel flag indicates whether or not to set the model's DepotPath property
var resetSearchModel = true;
var selectedPath = string.Empty;
var current = UIHelper.GetCurrentSelector();
if (current != null) {
if (current.SelectorType == SELECTOR_TYPE.WORKSPACE || current.SelectorType == SELECTOR_TYPE.SERVER) {
if (current.Model.CurrentSelection.Length == 1) {
if (current.Model.CurrentSelection[0] is FileItem) {
var p = current.Model.CurrentSelection[0].DepotPath;
if (p != Constants.DUMMY_DEPOT_PATH) {
selectedPath = p.Substring(0, p.LastIndexOf("/"));
}
} else {
selectedPath = current.Model.CurrentSelection[0].DepotPath;
}
} else if (current.Model.CurrentSelection.Length > 1) {
var p = current.Model.CurrentSelection[0].DepotPath;
if (p != Constants.DUMMY_DEPOT_PATH) {
selectedPath = p.Substring(0, p.LastIndexOf("/"));
}
}
} else if (current.SelectorType == SELECTOR_TYPE.SEARCH) {
// if we're on the search selector, then don't reset the search model's DepotPath
resetSearchModel = false;
}
}
if (selectedPath.StartsWith(Constants.DUMMY_DEPOT_PATH)) {
selectedPath = string.Empty;
} else if (selectedPath.Trim().Length < 3) {
selectedPath = string.Empty;
}
// otherwise, we are doing a search!
UIHelper.DeselectAllSelectors();
var searchSelector = UIHelper.GetSelector(SELECTOR_TYPE.SEARCH);
searchSelector.Model.IsSelected = true;
(searchSelector.Model as SearchSelectorViewModel).SearchStr = searchStr;
if (resetSearchModel) {
(searchSelector.Model as SearchSelectorViewModel).DepotPath = selectedPath;
}
searchSelector.Model.Refresh();
var main = App.Current.MainWindow as MainWindow;
main.ViewPane.Content = searchSelector.SidbebarView;
}
}
}
private void NewFolderButton_Click(object sender, RoutedEventArgs e) {
var main = App.Current.MainWindow as MainWindow;
var parentDir = GetCurrentWorkingDirectory();
if (!string.IsNullOrEmpty(parentDir)) {
var form = new NewFolderForm();
var dialog = main.DialogManager.CreateCustomContentDialog(form, DialogMode.OkCancel);
form.ParentDialog = dialog;
dialog.VerticalDialogAlignment = System.Windows.VerticalAlignment.Top;
dialog.OkText = "Create";
dialog.CanOk = false;
dialog.Ok = () => {
var newDir = form.NewDirName.Text;
if (newDir.Trim().Length > 0) {
var pathString = System.IO.Path.Combine(parentDir, newDir);
Directory.CreateDirectory(pathString);
UIHelper.RefreshSelectorAsync(SELECTOR_TYPE.WORKSPACE);
}
};
dialog.Show();
}
}
private void AddFilesButton_Click(object sender, RoutedEventArgs e) {
var main = App.Current.MainWindow as MainWindow;
var parentDir = GetCurrentWorkingDirectory();
if (!string.IsNullOrEmpty(parentDir)) {
var ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Multiselect = true;
if ((bool)ofd.ShowDialog()) {
var fileNames = ofd.FileNames;
if (fileNames != null && fileNames.Length > 0) {
foreach (var fn in fileNames) {
Utility.CopyFileToDirectory(fn, parentDir);
}
Utility.GetPerforceHelper().ReconcileFiles(parentDir + "\\...");
UIHelper.RefreshCurrent();
UIHelper.RefreshSelectorAsync(SELECTOR_TYPE.PENDING);
}
}
}
}
private string GetCurrentWorkingDirectory() {
var main = App.Current.MainWindow as MainWindow;
var parentDir = string.Empty;
var favSelector = main.Selectors.GetSelector(SELECTOR_TYPE.FAVORITE);
if (main.CurrentSelection.Length == 1) {
var item = main.CurrentSelection[0];
if (item is FolderItem) {
parentDir = item.ClientPath;
} else {
var fi = new FileInfo(item.ClientPath);
parentDir = fi.DirectoryName;
}
} else if (main.CurrentSelection.Length > 1) {
var item = main.CurrentSelection[0];
if (item is FolderItem) {
var di = new DirectoryInfo(item.ClientPath);
parentDir = di.Parent.FullName;
} else {
var fi = new FileInfo(item.ClientPath);
parentDir = fi.DirectoryName;
}
} else if (favSelector.Model.IsSelected) {
var path = (favSelector.Model as FavoritesViewModel).Item.FolderPath;
if (!path.StartsWith("//")) {
parentDir = path;
}
}
return parentDir;
}
private void NavRightButton_Click(object sender, RoutedEventArgs e) {
var main = App.Current.MainWindow as MainWindow;
var location = main.NavForward();
UIHelper.NavTo(location);
}
private void NavLeftButton_Click(object sender, RoutedEventArgs e) {
var main = App.Current.MainWindow as MainWindow;
var location = main.NavBack();
UIHelper.NavTo(location);
}
}
}