// // Copyright 2014 Perforce Software Inc. // using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Windows; namespace Perforce.ViewModel { public class LoginFormViewModel : ViewModelBase { private LocationItem _selectedLocation; private List _locationItems; private string _detailsText; private string _usernameText; private string _message; public void Init(string message = null) { _message = message; UsernameText = Environment.UserName; _locationItems = new List(); var configFile = AppDomain.CurrentDomain.BaseDirectory + "hosts.json"; if (File.Exists(configFile)) { var json = File.ReadAllText(configFile); _locationItems = JsonConvert.DeserializeObject>(json); } var prefs = Utility.GetUserPreferences(); if (!string.IsNullOrEmpty(prefs.LastP4Port)) { DetailsText = prefs.LastP4Port; Log.Debug(string.Format("LoginFormViewModel: setting DetailsText to {0}", prefs.LastP4Port)); } if (!string.IsNullOrEmpty(prefs.LastP4User)) { UsernameText = prefs.LastP4User; Log.Debug(string.Format("LoginFormViewModel: setting UsernameText to {0}", prefs.LastP4User)); } } public string Message { get { return _message; } set { _message = value; OnPropertyChanged("Message"); OnPropertyChanged("MessageVisibility"); } } public Visibility MessageVisibility { get { return String.IsNullOrEmpty(_message) ? Visibility.Collapsed : Visibility.Visible; } } public LocationItem SelectedLocation { get { return _selectedLocation; } set { _selectedLocation = value; OnPropertyChanged("SelectedLocation"); DetailsText = _selectedLocation.P4Port; } } public List LocationItems { get { return _locationItems; } set { _locationItems = value; OnPropertyChanged("LocationItems"); } } public string DetailsText { get { return _detailsText; } set { _detailsText = value; OnPropertyChanged("DetailsText"); } } public string UsernameText { get { return _usernameText; } set { _usernameText = value; OnPropertyChanged("UsernameText"); } } } public class LocationItem { public string Hostname { get; set; } public string P4Port { get; set; } } }