Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using System.Windows.Data;
- using System.Windows.Input;
- using LogControl9K.Windows.Util;
- using Microsoft.Win32;
- namespace LogControl9K.Windows.Controls.ViewModels {
- /// <summary>
- /// Main ViewModel of Log9KControl
- /// </summary>
- internal class Log9KControlViewModel : ViewModelBase {
- #region Properties
- private ICollectionView _logEntriesCollectionView;
- public ICollectionView LogEntriesCollectionView {
- get { return _logEntriesCollectionView; }
- private set {
- _logEntriesCollectionView = value;
- OnPropertyChanged("LogEntriesCollectionView");
- }
- }
- private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) {
- if (propertyChangedEventArgs.PropertyName == "CurrentTab") {
- LogEntriesCollectionView = CollectionViewSource.GetDefaultView(CurrentTab.LogEntryCollection);
- }
- }
- #region Tabs stuff
- public ObservableCollection<Log9KTab> LogTabs { get; private set; }
- private Log9KTab _currentTab;
- public Log9KTab CurrentTab {
- get {
- return _currentTab;
- }
- set {
- _currentTab = value;
- if (value.IsAllTab() || value.IsNoRepeatTab()) {
- IsAllOrNoRepeatTabCurrent = true;
- } else {
- IsAllOrNoRepeatTabCurrent = false;
- }
- OnPropertyChanged("CurrentTab");
- }
- }
- public Log9KTabAll TabAll {
- get { return Log9KCore.Instance.TabAll; }
- }
- public Log9KTabNoRepeat TabNoRepeat{
- get { return Log9KCore.Instance.TabNoRepeat; }
- }
- private bool _isAllOrNoRepeatTabCurrent;
- public bool IsAllOrNoRepeatTabCurrent {
- get {
- return _isAllOrNoRepeatTabCurrent;
- }
- set {
- _isAllOrNoRepeatTabCurrent = value;
- OnPropertyChanged("IsAllOrNoRepeatTabCurrent");
- }
- }
- #endregion
- #region Show/hidden
- #region Settings show/hidden
- private string _settingsButtonContent;
- /// <summary>
- /// Content of button which hides/shows settings
- /// </summary>
- public string SettingsButtonContent {
- get { return _settingsButtonContent; }
- private set {
- _settingsButtonContent = value;
- OnPropertyChanged("SettingsButtonContent");
- }
- }
- private bool _showSettings;
- /// <summary>
- /// Is settings showing or not
- /// </summary>
- public bool ShowSettings {
- get { return _showSettings; }
- private set {
- SettingsButtonContent = value ? ">" : "<";
- _showSettings = value;
- OnPropertyChanged("ShowSettings");
- }
- }
- #endregion
- private bool _showDuplicationsTree;
- /// <summary>
- /// Is settings showing or not
- /// </summary>
- public bool ShowDuplicationsTree {
- get { return _showDuplicationsTree; }
- set {
- _showDuplicationsTree = value;
- OnPropertyChanged("ShowDuplicationsTree");
- }
- }
- private bool _showDataGridControls;
- /// <summary>
- /// Is settings showing or not
- /// </summary>
- public bool ShowDataGridControls {
- get { return _showDataGridControls; }
- private set {
- _showDataGridControls = value;
- OnPropertyChanged("ShowDataGridControls");
- }
- }
- #endregion
- #region Commands
- /// <summary>
- /// Let the given entry be Selected
- /// </summary>
- public ICommand SelectEntryCommand { get; private set; }
- /// <summary>
- /// Filter the log entries by given time period
- /// </summary>
- public ICommand FilterByTimeCommand { get; private set; }
- /// <summary>
- /// Cancel time filter
- /// </summary>
- public ICommand CancelFilterByTimeCommand { get; private set; }
- /// <summary>
- /// Copy log entries to clipboard
- /// </summary>
- public ICommand CopyEntriesCommand { get; private set; }
- /// <summary>
- /// Save log entries to file
- /// </summary>
- public ICommand SaveEntriesCommand { get; private set; }
- /// <summary>
- /// Show or hide settings groupbox
- /// </summary>
- public ICommand ShowOrHideSettingsCommand { get; private set; }
- /// <summary>
- /// Show or hide data grid's controls
- /// </summary>
- public ICommand ShowOrHideDataGridControlsCommand { get; private set; }
- public ICommand StartRemovingFromBottomCommand { get; private set; }
- public ICommand StartRemovingFromTopCommand { get; private set; }
- public ICommand LoadOlderLogCommand { get; private set; }
- public ICommand LoadNewerLogCommand { get; private set; }
- public ICommand LoadNewerLogArrayCommand { get; private set; }
- public ICommand LoadOlderLogArrayCommand { get; private set; }
- public ICommand SortLogEntriesCollectionCommand { get; private set; }
- public ICommand ShowLastLogsCommand { get; private set; }
- public ICommand LoadStartingFromIDCommand { get; private set; }
- public ICommand SelectLogEntryCommand { get; private set; }
- #endregion
- #region Selected entries stuff
- private Log9KEntry _selectedEntry;
- public Log9KEntry SelectedEntry {
- get { return _selectedEntry; }
- set {
- _selectedEntry = value;
- OnPropertyChanged("SelectedEntry");
- }
- }
- private Log9KEntry[] _selectedEntries;
- public Log9KEntry[] SelectedEntries {
- get { return _selectedEntries; }
- set {
- _selectedEntries = value;
- OnPropertyChanged("SelectedEntries");
- }
- }
- #endregion
- public Log9KSettings Settings { get { return Log9KCore.Settings; } }
- private IEnumerable _logEntryTypes = Log9KUtil.EnumUtil.GetValues<Log9KEntry.LogEntryTypes>();
- public IEnumerable LogEntryTypes { get { return _logEntryTypes; } }
- private string _pageUpDownToLoad = 10.ToString();
- public string PageUpDownToLoad {
- get { return _pageUpDownToLoad; }
- set {
- _pageUpDownToLoad = value;
- OnPropertyChanged("PageUpDownToLoad");
- }
- }
- #region Time filter
- private string _filterTimeStart;
- public string FilterTimeStart {
- get { return _filterTimeStart; }
- set {
- _filterTimeStart = value;
- OnPropertyChanged("FilterTimeStart");
- }
- }
- private string _filterTimeEnd;
- public string FilterTimeEnd {
- get { return _filterTimeEnd; }
- set {
- _filterTimeEnd = value;
- OnPropertyChanged("FilterTimeEnd");
- }
- }
- public object IsDebug {
- get {
- #if DEBUG
- return true;
- #else
- return false;
- #endif
- }
- }
- #endregion
- #endregion
- #region Fields
- private DateTime _startDateTime;
- private DateTime _endDateTime;
- private bool _timeStartSuccess;
- private bool _timeEndSuccess;
- #endregion
- #region Constructor
- public Log9KControlViewModel() {
- InitCommands();
- LogTabs = Log9KCore.Instance.Log9KTabsCollection;
- ShowSettings = false;
- ShowDataGridControls = true;
- PropertyChanged += OnPropertyChanged;
- }
- private void InitCommands() {
- SelectEntryCommand = new sCommand(SelectLogEntryAction);
- FilterByTimeCommand = new sCommand(FilterByTimeAction);
- CancelFilterByTimeCommand = new sCommand(CancelFilterByTimeAction);
- CopyEntriesCommand = new sCommand(CopyEntriesAction);
- SaveEntriesCommand = new sCommand(SaveEntriesAction);
- ShowOrHideSettingsCommand = new sCommand(ShowOrHideSettingsAction);
- ShowOrHideDataGridControlsCommand = new sCommand(ShowOrHideDataGridControlsAction);
- StartRemovingFromBottomCommand = new sCommand(StartRemovingFromBottomAction);
- StartRemovingFromTopCommand = new sCommand(StartRemovingFromTopAction);
- LoadOlderLogCommand = new sCommand(LoadOlderLogAction);
- LoadOlderLogArrayCommand = new sCommand(LoadOlderLogArrayAction);
- LoadNewerLogCommand = new sCommand(LoadNewerLogAction);
- LoadNewerLogArrayCommand = new sCommand(LoadNewerLogArrayAction);
- SortLogEntriesCollectionCommand = new sCommand(SortLogEntriesCollectionAction);
- ShowLastLogsCommand = new sCommand(ShowLastLogsAction);
- LoadStartingFromIDCommand = new sCommand(LoadStartingFromIDAction);
- SelectLogEntryCommand = new sCommand(SelectLogEntryAction);
- }
- #endregion
- #region Private methods
- private void ShowOrHideDataGridControlsAction() {
- ShowDataGridControls = !ShowDataGridControls;
- }
- private void LoadStartingFromIDAction(object id) {
- if (!CurrentTab.IsAllTab()) {
- return;
- }
- uint startFromID;
- uint i = id is uint ? (uint) id : 0;
- if (i == 0) {
- string s = id as string;
- if (s == null) {
- return;
- }
- if (!uint.TryParse(s, out startFromID)) {
- return;
- }
- } else {
- startFromID = i;
- }
- BackgroundWorker bw = CurrentTab.LoadEntries(startFromID);
- // let's select loaded entry
- if (bw != null) {
- bw.RunWorkerCompleted += (sender, args) => {
- foreach (Log9KEntry entry in LogEntriesCollectionView.Cast<Log9KEntry>().Where(entry => entry.ID == startFromID)) {
- CurrentTab.SelectedEntry = entry;
- }
- };
- }
- }
- private void LoadOlderLogArrayAction() {
- Log9KUtil.ExecuteCommand(StartRemovingFromBottomCommand);
- int pageUpDownToLoad;
- bool success = int.TryParse(PageUpDownToLoad, out pageUpDownToLoad);
- if (!success) {
- pageUpDownToLoad = 10;
- }
- for (int i = 0; i < pageUpDownToLoad; i++) {
- CurrentTab.LoadOlderLogEntry();
- }
- SelectedEntry = CurrentTab.LogEntryCollection[0];
- }
- private void LoadNewerLogArrayAction() {
- Log9KUtil.ExecuteCommand(StartRemovingFromTopCommand);
- int pageUpDownToLoad;
- bool success = int.TryParse(PageUpDownToLoad, out pageUpDownToLoad);
- if (!success) {
- pageUpDownToLoad = 10;
- }
- for (int i = 0; i < pageUpDownToLoad; i++) {
- CurrentTab.LoadNewerLogEntry();
- }
- SelectedEntry = CurrentTab.LogEntryCollection[CurrentTab.LogEntryCollection.Count - 1];
- }
- private void SortLogEntriesCollectionAction() {
- CurrentTab.LogEntryCollection.BubbleSort();
- }
- private void StartRemovingFromBottomAction() {
- CurrentTab.LogEntryCollection.StartRemovingFromBottom();
- }
- private void StartRemovingFromTopAction() {
- CurrentTab.LogEntryCollection.StartRemovingFromTop();
- }
- private void LoadOlderLogAction() {
- for (int i = 0; i < 25; i++) {
- bool s = CurrentTab.LoadOlderLogEntry();
- if (s) {
- break;
- }
- }
- }
- private void LoadNewerLogAction() {
- CurrentTab.LoadNewerLogEntry();
- }
- private void ShowLastLogsAction(object parameter) {
- if (!CurrentTab.IsAllTab()) {
- return;
- }
- bool clearCollection = parameter as bool? ?? false;
- CurrentTab.ShowLastLogs(clearCollection);
- }
- private void SelectLogEntryAction(object entry) {
- Log9KEntry log9KEntry = entry as Log9KEntry;
- if (log9KEntry != null) {
- int d = log9KEntry.GetDuplicationHash();
- var a = CurrentTab.DuplicationsDictionary[d];
- // SelectedEntry = log9KEntry;
- }
- }
- private void ShowOrHideSettingsAction() {
- ShowSettings = !ShowSettings;
- }
- #region Copy and save entries
- /// <summary>
- /// Copy selected entries to clipboard
- /// </summary>
- /// <param name="o"></param>
- private void CopyEntriesAction(object o) {
- string clipboard = GetStringOfSelectedEntries(o);
- Clipboard.SetText(clipboard);
- }
- /// <summary>
- /// Manual save selected entries to file
- /// </summary>
- /// <param name="o"></param>
- private void SaveEntriesAction(object o) {
- string s = GetStringOfSelectedEntries(o);
- SaveFileDialog sfd = new SaveFileDialog {
- AddExtension = true,
- DefaultExt = ".log",
- CheckPathExists = true,
- Filter = "Log Files|*.log",
- InitialDirectory = Path.GetFullPath(Settings.Folder)
- };
- sfd.ShowDialog();
- string filename = sfd.FileName;
- if (filename == "") {
- return;
- }
- if (File.Exists(filename)) {
- File.Delete(filename);
- }
- File.WriteAllText(filename, s);
- }
- /// <summary>
- /// Get selected entries as one string
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- private string GetStringOfSelectedEntries(object o) {
- string s = "";
- IList selectedEntries = GetSelectedEntries(o);
- foreach (Log9KEntry e in selectedEntries) {
- s += e + Environment.NewLine;
- }
- return s;
- }
- /// <summary>
- /// Checks is given object a list and returns it casted if so
- /// </summary>
- /// <param name="o"></param>
- /// <returns>returns list of selected log entries</returns>
- private IList GetSelectedEntries(object o) {
- if (o == null) {
- return null;
- }
- IList selectedEntries = o as IList;
- if (selectedEntries == null) {
- return null;
- }
- return selectedEntries;
- }
- #endregion
- #region Time filter
- private void CancelFilterByTimeAction() {
- if (CurrentTab.IsAllTab()) {
- CurrentTab.ShowLastLogs(true);
- } else {
- LogEntriesCollectionView.Filter = null;
- }
- }
- /// <summary>
- /// Set time filter for collection view
- /// </summary>
- private void FilterByTimeAction(object timeEnd) {
- bool a = false;
- if (timeEnd != null) {
- a = timeEnd is string;
- }
- if (!a) {
- _timeEndSuccess = DateTime.TryParse(FilterTimeEnd, out _endDateTime);
- } else {
- _timeEndSuccess = DateTime.TryParse((string)timeEnd, out _endDateTime);
- }
- _timeStartSuccess = DateTime.TryParse(FilterTimeStart, out _startDateTime);
- if (!(_timeStartSuccess || _timeEndSuccess)) {
- LogEntriesCollectionView.Filter = null;
- return;
- }
- if (CurrentTab.IsAllTab()) {
- CurrentTab.FilterByTime(_startDateTime, _endDateTime);
- } else {
- LogEntriesCollectionView.Filter = Filter;
- }
- }
- /// <summary>
- /// Time filter for collection view
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- private bool Filter(object o) {
- if (!(o is Log9KEntry)) {
- return false;
- }
- Log9KEntry entry = (Log9KEntry) o;
- DateTime entryTime= entry.Time.ToDateTime();
- if (!_timeStartSuccess) {
- if (entryTime.CompareTo(_endDateTime) <= 0) {
- return true;
- }
- }
- if (!_timeEndSuccess) {
- if (entryTime.CompareTo(_startDateTime) >= 0) {
- return true;
- }
- }
- if (entryTime.CompareTo(_startDateTime) >= 0 && (entryTime.CompareTo(_endDateTime) <= 0)) {
- return true;
- }
- return false;
- }
- #endregion
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement