//
// Copyright 2014 Perforce Software Inc.
//
using Perforce.Helper;
using Perforce.Model;
using Perforce.View;
using Perforce.ViewModel;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Technewlogic.WpfDialogManagement;
using Technewlogic.WpfDialogManagement.Contracts;
namespace Perforce {
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window {
private DialogManager _dialogManager;
private ICustomContentDialog loginDialog;
private SelectWorkspaceForm selectWorkspaceForm;
private ICustomContentDialog selectWorkspaceDialog;
private CreateWorkspaceForm createWorkspaceForm;
private ICustomContentDialog createWorkspaceDialog;
private bool _autoLoggedIn = false;
#region Constructor
public MainWindow() {
InitializeComponent();
// set up the dialog manager
_dialogManager = new DialogManager(this, Dispatcher);
var prefs = Utility.GetUserPreferences();
// restore the window dimensions to those in the preferences
this.Height = prefs.WindowHeight;
this.Width = prefs.WindowWidth;
this.Top = prefs.WindowTop;
this.Left = prefs.WindowLeft;
this.WindowState = prefs.WindowState;
var helper = Utility.GetPerforceHelper();
if (helper == null || !helper.IsLoggedIn()) {
if (helper != null) {
helper.Disconnect();
helper.Dispose();
}
Utility.ClearProperty(Constants.PERFORCE_HELPER);
ShowLoginDialog();
} else if (!helper.ClientEnabled) {
ShowWorkspaceDialog();
} else {
_autoLoggedIn = true;
Utility.StartBackgroundProcesses();
}
}
#endregion
#region Accessors
public DialogManager DialogManager {
get { return _dialogManager; }
}
#endregion
#region Login / Workspace Dialogs
public void ShowLoginDialog(string msg = null) {
var loginForm = new LoginForm(msg);
loginDialog = _dialogManager.CreateCustomContentDialog(loginForm, "Login", DialogMode.OkCancel);
loginDialog.VerticalDialogAlignment = System.Windows.VerticalAlignment.Top;
loginDialog.OkText = "Login";
loginDialog.Ok = () => {
var userPrefs = Utility.GetUserPreferences();
var serverUri = loginForm.DetailsText.Text.Trim();
if (!Utility.CheckTCPConnection(serverUri)) {
ShowLoginDialog("Cannot reach server");
} else {
if (!string.IsNullOrEmpty(serverUri)) {
userPrefs.LastP4Port = serverUri;
userPrefs.SaveProperty(Constants.PREFS_LASTP4PORT);
}
var username = loginForm.UsernameText.Text.Trim();
if (!string.IsNullOrEmpty(username)) {
userPrefs.LastP4User = username;
userPrefs.SaveProperty(Constants.PREFS_LASTP4USER);
}
var password = loginForm.PasswordText.Password.Trim();
var helper = new PerforceHelper(serverUri, username);
Utility.SetProperty(Constants.PERFORCE_HELPER, helper);
if (helper.Login(password).Item1) {
userPrefs.LastP4Ticket = helper.Ticket;
userPrefs.SaveProperty(Constants.PREFS_LASTP4TICKET);
// next we choose the workspace
ShowWorkspaceDialog();
} else {
// login failed... try again
ShowLoginDialog("Login failed");
}
}
};
loginDialog.CanOk = false;
loginDialog.Cancel = () => {
if(selectWorkspaceDialog != null) selectWorkspaceDialog.Close();
if(createWorkspaceDialog != null) createWorkspaceDialog.Close();
App.Current.Shutdown();
};
loginForm.ParentDialog = loginDialog;
loginDialog.Show();
}
public void ShowWorkspaceDialog() {
var helper = Utility.GetPerforceHelper(check: false);
var currentClient = string.Empty;
if (helper.ClientEnabled) {
currentClient = helper.CurrentClient.Name;
}
selectWorkspaceForm = new SelectWorkspaceForm();
selectWorkspaceDialog = _dialogManager.CreateCustomContentDialog(selectWorkspaceForm, DialogMode.YesNoCancel);
selectWorkspaceDialog.VerticalDialogAlignment = System.Windows.VerticalAlignment.Top;
selectWorkspaceDialog.YesText = "Choose";
selectWorkspaceDialog.Yes = () => {
Utility.StopBackgroundProcesses();
createWorkspaceDialog.Close();
// get the chosen workspace info
var choice = selectWorkspaceForm.WorkspaceChoice.SelectedItem as Perforce.ViewModel.WorkspaceItem;
// set the workspace in the helper
helper.SetClient(choice.Name);
// clean up the changelist in the workspace, if needed
helper.CleanChangelist();
// gather up changes in this workspace into one changelist
// commented out for now
// helper.GatherOpenFilesInCurrentChangelist();
// reset the workspace view
var wsSelector = UIHelper.GetSelector(SELECTOR_TYPE.WORKSPACE);
if (wsSelector != null) {
GridHelper.ClearGrid(wsSelector.SidbebarView.ListingGrid);
var currentSelector = UIHelper.GetCurrentSelector();
if (currentSelector != null && currentSelector.SelectorType == SELECTOR_TYPE.WORKSPACE) {
UIHelper.DeselectAllSelectors();
GridHelper.ClearGrid(wsSelector.SidbebarView.ListingGrid);
wsSelector.Model.CurrentColumn = null;
wsSelector.Model.CurrentSelection = null;
Breadcrumbs.Model.UpdatePath(null);
}
}
// save the workspace in the user preferences
var userPrefs = Utility.GetUserPreferences();
userPrefs.LastP4Client = choice.Name;
userPrefs.SaveProperty(Constants.PREFS_LASTP4CLIENT);
// refresh the UI components
Selectors.Refresh();
Tools.Refresh();
Menus.Refresh();
Utility.SetupWorkspaceWatcher();
Utility.SetupSearchHelper();
Utility.StartBackgroundProcesses();
};
selectWorkspaceDialog.CanYes = false;
selectWorkspaceDialog.NoText = "New Workspace";
selectWorkspaceDialog.Cancel = () => {
if (string.IsNullOrEmpty(currentClient)) {
Selectors.WorkspaceControl.IsEnabled = false;
}
createWorkspaceDialog.Close();
};
selectWorkspaceForm.ParentDialog = selectWorkspaceDialog;
createWorkspaceForm = new CreateWorkspaceForm();
createWorkspaceDialog = _dialogManager.CreateCustomContentDialog(createWorkspaceForm, DialogMode.OkCancel);
createWorkspaceDialog.VerticalDialogAlignment = System.Windows.VerticalAlignment.Top;
createWorkspaceDialog.OkText = "Create";
createWorkspaceDialog.Ok = () => {
var name = createWorkspaceForm.WorkspaceNameText.Text;
var root = createWorkspaceForm.WorkspaceRootText.Text;
if (string.IsNullOrEmpty(name)) {
MessageBox.Show("Sorry, workspaces must have names");
Selectors.WorkspaceControl.IsEnabled = false;
createWorkspaceDialog.Close();
return;
}
if (string.IsNullOrEmpty(root)) {
MessageBox.Show("Workspace root is not specified");
Selectors.WorkspaceControl.IsEnabled = false;
createWorkspaceDialog.Close();
return;
}
helper.CreateClient(name, root, "workspace created by Perforce");
};
createWorkspaceDialog.CanOk = false;
createWorkspaceDialog.Cancel = () => {
if (string.IsNullOrEmpty(currentClient)) {
Selectors.WorkspaceControl.IsEnabled = false;
}
createWorkspaceDialog.Close();
};
createWorkspaceForm.ParentDialog = createWorkspaceDialog;
createWorkspaceDialog.Show();
selectWorkspaceDialog.Show();
}
#endregion
#region Cleanup
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
var userPrefs = Utility.GetUserPreferences();
if (userPrefs == null) {
userPrefs = new UserPreferences();
}
userPrefs.WindowHeight = this.Height;
userPrefs.WindowWidth = this.Width;
userPrefs.WindowTop = this.Top;
userPrefs.WindowLeft = this.Left;
userPrefs.WindowState = this.WindowState;
userPrefs.Save();
Utility.GetLogWindow().Close();
}
#endregion
public void Sidebar_Label_Clicked(SidebarSelector currentSelector, RoutedEventArgs e) {
if (currentSelector.SelectorType != SELECTOR_TYPE.FAVORITE) {
UIHelper.ClearSelectedFavorites();
}
// switch to the sidebar view, initialize the grid if necessary
if (currentSelector.SidbebarView.ListingGrid == null || currentSelector.SidbebarView.ListingGrid.Children.Count == 0) {
GridHelper.InitializeGrid(currentSelector);
}
if (currentSelector.SelectorType == SELECTOR_TYPE.PENDING) {
UIHelper.RefreshSelector(SELECTOR_TYPE.PENDING);
}
ViewPane.Content = currentSelector.SidbebarView;
var ni = new NavItem { Selector = currentSelector.SelectorType, Path = string.Empty };
if (!ni.Equals(_currentNavItem)) {
if (_currentNavItem != null) _previousStack.Push(_currentNavItem);
_currentNavItem = ni;
}
currentSelector.Model.IsRefreshable = true;
currentSelector.Model.IsSelected = true;
RefreshBreadcrumbs();
Tools.Model.Refresh();
}
public void HandleListingItemClick(ListingItem currentItem) {
//var selector = UIHelper.GetCurrentSelector();
var selector = currentItem.ColumnContext.Selector;
var view = ViewPane.Content as SidebarView;
var grid = view.ListingGrid;
var numColumns = grid.ColumnDefinitions.Count;
var currentColumn = currentItem.ColumnContext.Model.ColumnNumber;
var nextColumn = currentColumn + 1;
for (var i = nextColumn; i < numColumns; i++) {
grid.Children.RemoveAt(nextColumn);
grid.ColumnDefinitions.RemoveAt(nextColumn);
}
// handle folders differntly than files
if (currentItem.Type.Equals(ListingItem.TYPE.FOLDER)) {
GridHelper.SetColumnInGrid(selector, currentItem);
if (_currentNavItem != null) {
_previousStack.Push(_currentNavItem);
}
var path = string.Empty;
if (currentItem.DepotPath != null && !currentItem.DepotPath.StartsWith(Constants.DUMMY_DEPOT_PATH)) {
path = currentItem.DepotPath;
} else {
path = currentItem.ClientPath;
}
_currentNavItem = new NavItem { Selector = selector.SelectorType, Path = path };
} else {
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
var itemInfo = new ListingItemInfo(currentItem);
grid.Children.Add(itemInfo);
Grid.SetColumn(itemInfo, nextColumn);
}
GridHelper.AddDummyColumnToGrid(grid);
Tools.Model.Refresh();
}
public void DisplayCurrentItemVersionInfo(FileItem item = null) {
UIHelper.SetBusyState();
if (CurrentSelection.Length == 1) {
if (item == null) {
item = CurrentSelection[0] as FileItem;
}
var view = ViewPane.Content as SidebarView;
var grid = view.ListingGrid;
GridHelper.ShowVersionInfo(grid, item, dummy: true);
}
}
public void DisplayCurrentFolderHistory(FolderItem folder = null) {
UIHelper.SetBusyState();
if (CurrentSelection.Length == 1 && CurrentSelection[0] is FolderItem) {
if (folder == null) {
folder = CurrentSelection[0] as FolderItem;
}
var view = ViewPane.Content as SidebarView;
var grid = view.ListingGrid;
GridHelper.ShowFolderHistory(grid, folder, dummy: true);
}
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
Selectors.Refresh();
Tools.Refresh();
Menus.Refresh();
if (_autoLoggedIn) {
CheckCommandLineURL();
}
}
private bool _ignoreArgs = false;
private void CheckCommandLineURL() {
var args = Environment.GetCommandLineArgs();
if (!_ignoreArgs && args.Length > 1) {
var path = args[1];
if (path.StartsWith("p4://")) {
UIHelper.GoToPath(path);
}
_ignoreArgs = true;
}
}
public ListingItem[] CurrentSelection {
get {
var currentSelector = UIHelper.GetCurrentSelector();
if (currentSelector != null) {
return currentSelector.Model.CurrentSelection;
} else {
return null;
}
}
}
public void RefreshBreadcrumbs() {
Breadcrumbs.Model.UpdatePath(CurrentSelection);
}
public void RefreshCurrent() {
foreach (var item in CurrentSelection) {
item.Refresh();
}
}
private Stack _previousStack = new Stack();
private NavItem _currentNavItem;
private Stack _nextStack = new Stack();
public NavItem CurrentNavItem {
get { return _currentNavItem; }
set {
_currentNavItem = value;
}
}
public Stack PreviousStack {
get { return _previousStack; }
}
public Stack NextStack {
get { return _nextStack; }
}
public NavItem NavForward() {
_previousStack.Push(_currentNavItem);
_currentNavItem = _nextStack.Pop();
return _currentNavItem;
}
public NavItem NavBack() {
_nextStack.Push(_currentNavItem);
_currentNavItem = _previousStack.Pop();
return _currentNavItem;
}
private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) {
// ctrl-F should change focus to the search field, if
if (e.Key == Key.F && Keyboard.Modifiers == ModifierKeys.Control) {
if (Tools.SearchTextbox.IsVisible) {
Tools.SearchTextbox.Focus();
e.Handled = true;
}
}
}
private void Selectors_Loaded(object sender, RoutedEventArgs e) {
CheckCommandLineURL();
}
}
}