Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.05 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Windows;
  9. using System.Windows.Data;
  10. using System.Windows.Input;
  11. using LogControl9K.Windows.Util;
  12. using Microsoft.Win32;
  13.  
  14. namespace LogControl9K.Windows.Controls.ViewModels {
  15.     /// <summary>
  16.     /// Main ViewModel of Log9KControl
  17.     /// </summary>
  18.     internal class Log9KControlViewModel : ViewModelBase {
  19.  
  20.        
  21.         #region Properties
  22.  
  23.         private ICollectionView _logEntriesCollectionView;
  24.         public ICollectionView LogEntriesCollectionView {
  25.             get { return _logEntriesCollectionView; }
  26.             private set {
  27.                 _logEntriesCollectionView = value;
  28.                 OnPropertyChanged("LogEntriesCollectionView");
  29.             }
  30.         }
  31.         private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) {
  32.             if (propertyChangedEventArgs.PropertyName == "CurrentTab") {
  33.                 LogEntriesCollectionView = CollectionViewSource.GetDefaultView(CurrentTab.LogEntryCollection);
  34.             }
  35.         }
  36.        
  37.  
  38.  
  39.         #region Tabs stuff
  40.  
  41.         public ObservableCollection<Log9KTab> LogTabs { get; private set; }
  42.  
  43.         private Log9KTab _currentTab;
  44.         public Log9KTab CurrentTab {
  45.             get {
  46.                 return _currentTab;
  47.             }
  48.             set {
  49.                 _currentTab = value;
  50.                 if (value.IsAllTab() || value.IsNoRepeatTab()) {
  51.                     IsAllOrNoRepeatTabCurrent = true;
  52.                 } else {
  53.                     IsAllOrNoRepeatTabCurrent = false;
  54.                 }
  55.                 OnPropertyChanged("CurrentTab");
  56.             }
  57.         }
  58.  
  59.         public Log9KTabAll TabAll {
  60.             get { return Log9KCore.Instance.TabAll; }
  61.         }
  62.  
  63.         public Log9KTabNoRepeat TabNoRepeat{
  64.             get { return Log9KCore.Instance.TabNoRepeat; }
  65.         }
  66.  
  67.         private bool _isAllOrNoRepeatTabCurrent;
  68.         public bool IsAllOrNoRepeatTabCurrent {
  69.             get {
  70.                 return _isAllOrNoRepeatTabCurrent;
  71.             }
  72.             set {
  73.                 _isAllOrNoRepeatTabCurrent = value;
  74.                 OnPropertyChanged("IsAllOrNoRepeatTabCurrent");
  75.             }
  76.         }
  77.  
  78.         #endregion
  79.  
  80.            
  81.         #region Show/hidden
  82.  
  83.  
  84.         #region Settings show/hidden
  85.  
  86.         private string _settingsButtonContent;
  87.         /// <summary>
  88.         /// Content of button which hides/shows settings
  89.         /// </summary>
  90.         public string SettingsButtonContent {
  91.             get { return _settingsButtonContent; }
  92.             private set {
  93.                 _settingsButtonContent = value;
  94.                 OnPropertyChanged("SettingsButtonContent");
  95.             }
  96.         }
  97.  
  98.  
  99.         private bool _showSettings;
  100.         /// <summary>
  101.         /// Is settings showing or not
  102.         /// </summary>
  103.         public bool ShowSettings {
  104.             get { return _showSettings; }
  105.             private set {
  106.                 SettingsButtonContent = value ? ">" : "<";
  107.                 _showSettings = value;
  108.                 OnPropertyChanged("ShowSettings");
  109.             }
  110.         }
  111.        
  112.         #endregion
  113.  
  114.        
  115.         private bool _showDuplicationsTree;
  116.         /// <summary>
  117.         /// Is settings showing or not
  118.         /// </summary>
  119.         public bool ShowDuplicationsTree {
  120.             get { return _showDuplicationsTree; }
  121.             set {
  122.                 _showDuplicationsTree = value;
  123.                 OnPropertyChanged("ShowDuplicationsTree");
  124.             }
  125.         }
  126.  
  127.         private bool _showDataGridControls;
  128.         /// <summary>
  129.         /// Is settings showing or not
  130.         /// </summary>
  131.         public bool ShowDataGridControls {
  132.             get { return _showDataGridControls; }
  133.             private set {
  134.                 _showDataGridControls = value;
  135.                 OnPropertyChanged("ShowDataGridControls");
  136.             }
  137.         }
  138.        
  139.         #endregion
  140.  
  141.  
  142.         #region Commands
  143.  
  144.         /// <summary>
  145.         /// Let the given entry be Selected
  146.         /// </summary>
  147.         public ICommand SelectEntryCommand { get; private set; }
  148.        
  149.         /// <summary>
  150.         /// Filter the log entries by given time period
  151.         /// </summary>
  152.         public ICommand FilterByTimeCommand { get; private set; }
  153.        
  154.         /// <summary>
  155.         /// Cancel time filter
  156.         /// </summary>
  157.         public ICommand CancelFilterByTimeCommand { get; private set; }
  158.        
  159.         /// <summary>
  160.         /// Copy log entries to clipboard
  161.         /// </summary>
  162.         public ICommand CopyEntriesCommand { get; private set; }
  163.  
  164.         /// <summary>
  165.         /// Save log entries to file
  166.         /// </summary>
  167.         public ICommand SaveEntriesCommand { get; private set; }
  168.        
  169.         /// <summary>
  170.         /// Show or hide settings groupbox
  171.         /// </summary>
  172.         public ICommand ShowOrHideSettingsCommand { get; private set; }
  173.        
  174.         /// <summary>
  175.         /// Show or hide data grid's controls
  176.         /// </summary>
  177.         public ICommand ShowOrHideDataGridControlsCommand { get; private set; }
  178.  
  179.         public ICommand StartRemovingFromBottomCommand { get; private set; }
  180.         public ICommand StartRemovingFromTopCommand { get; private set; }
  181.  
  182.         public ICommand LoadOlderLogCommand { get; private set; }
  183.         public ICommand LoadNewerLogCommand { get; private set; }
  184.  
  185.         public ICommand LoadNewerLogArrayCommand { get; private set; }
  186.         public ICommand LoadOlderLogArrayCommand { get; private set; }
  187.  
  188.         public ICommand SortLogEntriesCollectionCommand { get; private set; }
  189.  
  190.         public ICommand ShowLastLogsCommand { get; private set; }
  191.        
  192.         public ICommand LoadStartingFromIDCommand { get; private set; }
  193.  
  194.         public ICommand SelectLogEntryCommand { get; private set; }
  195.  
  196.         #endregion
  197.        
  198.        
  199.         #region Selected entries stuff
  200.  
  201.         private Log9KEntry _selectedEntry;
  202.         public Log9KEntry SelectedEntry {
  203.             get { return _selectedEntry; }
  204.             set {
  205.                 _selectedEntry = value;
  206.                 OnPropertyChanged("SelectedEntry");
  207.             }
  208.         }
  209.  
  210.         private Log9KEntry[] _selectedEntries;
  211.         public Log9KEntry[] SelectedEntries {
  212.             get { return _selectedEntries; }
  213.             set {
  214.                 _selectedEntries = value;
  215.                 OnPropertyChanged("SelectedEntries");
  216.             }
  217.         }
  218.  
  219.         #endregion
  220.        
  221.  
  222.         public Log9KSettings Settings { get { return Log9KCore.Settings; } }
  223.  
  224.         private IEnumerable _logEntryTypes = Log9KUtil.EnumUtil.GetValues<Log9KEntry.LogEntryTypes>();
  225.         public IEnumerable LogEntryTypes { get { return _logEntryTypes; } }
  226.  
  227.         private string _pageUpDownToLoad = 10.ToString();
  228.         public string PageUpDownToLoad {
  229.             get { return _pageUpDownToLoad; }
  230.             set {
  231.                 _pageUpDownToLoad = value;
  232.                 OnPropertyChanged("PageUpDownToLoad");
  233.             }
  234.         }
  235.  
  236.  
  237.        
  238.         #region Time filter
  239.  
  240.         private string _filterTimeStart;
  241.         public string FilterTimeStart {
  242.             get { return _filterTimeStart; }
  243.             set {
  244.                 _filterTimeStart = value;
  245.                 OnPropertyChanged("FilterTimeStart");
  246.             }
  247.         }
  248.  
  249.  
  250.         private string _filterTimeEnd;
  251.         public string FilterTimeEnd {
  252.             get { return _filterTimeEnd; }
  253.             set {
  254.                 _filterTimeEnd = value;
  255.                 OnPropertyChanged("FilterTimeEnd");
  256.             }
  257.         }
  258.  
  259.         public object IsDebug {
  260.             get {
  261. #if DEBUG
  262.                 return true;
  263. #else
  264.                 return false;
  265. #endif
  266.             }
  267.         }
  268.  
  269.         #endregion
  270.        
  271.  
  272.         #endregion
  273.  
  274.  
  275.         #region Fields
  276.  
  277.         private DateTime _startDateTime;
  278.         private DateTime _endDateTime;
  279.         private bool _timeStartSuccess;
  280.         private bool _timeEndSuccess;
  281.  
  282.         #endregion
  283.  
  284.  
  285.         #region Constructor
  286.  
  287.         public Log9KControlViewModel() {
  288.             InitCommands();
  289.  
  290.             LogTabs = Log9KCore.Instance.Log9KTabsCollection;
  291.  
  292.             ShowSettings = false;
  293.             ShowDataGridControls = true;
  294.             PropertyChanged += OnPropertyChanged;
  295.         }
  296.  
  297.         private void InitCommands() {
  298.             SelectEntryCommand = new sCommand(SelectLogEntryAction);
  299.             FilterByTimeCommand = new sCommand(FilterByTimeAction);
  300.             CancelFilterByTimeCommand = new sCommand(CancelFilterByTimeAction);
  301.             CopyEntriesCommand = new sCommand(CopyEntriesAction);
  302.             SaveEntriesCommand = new sCommand(SaveEntriesAction);
  303.             ShowOrHideSettingsCommand = new sCommand(ShowOrHideSettingsAction);
  304.            
  305.             ShowOrHideDataGridControlsCommand = new sCommand(ShowOrHideDataGridControlsAction);
  306.  
  307.             StartRemovingFromBottomCommand = new sCommand(StartRemovingFromBottomAction);
  308.             StartRemovingFromTopCommand = new sCommand(StartRemovingFromTopAction);
  309.  
  310.             LoadOlderLogCommand = new sCommand(LoadOlderLogAction);
  311.             LoadOlderLogArrayCommand = new sCommand(LoadOlderLogArrayAction);
  312.  
  313.             LoadNewerLogCommand = new sCommand(LoadNewerLogAction);
  314.             LoadNewerLogArrayCommand = new sCommand(LoadNewerLogArrayAction);
  315.            
  316.             SortLogEntriesCollectionCommand = new sCommand(SortLogEntriesCollectionAction);
  317.             ShowLastLogsCommand = new sCommand(ShowLastLogsAction);
  318.  
  319.             LoadStartingFromIDCommand = new sCommand(LoadStartingFromIDAction);
  320.  
  321.             SelectLogEntryCommand = new sCommand(SelectLogEntryAction);
  322.         }
  323.  
  324.  
  325.         #endregion
  326.        
  327.  
  328.  
  329.  
  330.         #region Private methods
  331.  
  332.         private void ShowOrHideDataGridControlsAction() {
  333.             ShowDataGridControls = !ShowDataGridControls;
  334.         }
  335.  
  336.         private void LoadStartingFromIDAction(object id) {
  337.             if (!CurrentTab.IsAllTab()) {
  338.                 return;
  339.             }
  340.             uint startFromID;
  341.             uint i = id is uint ? (uint) id : 0;
  342.             if (i == 0) {
  343.                 string s = id as string;
  344.                 if (s == null) {
  345.                     return;
  346.                 }
  347.                 if (!uint.TryParse(s, out startFromID)) {
  348.                     return;
  349.                 }
  350.             } else {
  351.                 startFromID = i;
  352.             }
  353.  
  354.             BackgroundWorker bw = CurrentTab.LoadEntries(startFromID);
  355.             // let's select loaded entry
  356.             if (bw != null) {
  357.                 bw.RunWorkerCompleted += (sender, args) => {
  358.                     foreach (Log9KEntry entry in LogEntriesCollectionView.Cast<Log9KEntry>().Where(entry => entry.ID == startFromID)) {
  359.                         CurrentTab.SelectedEntry = entry;
  360.                     }
  361.                 };
  362.                
  363.             }
  364.         }
  365.  
  366.         private void LoadOlderLogArrayAction() {
  367.             Log9KUtil.ExecuteCommand(StartRemovingFromBottomCommand);
  368.             int pageUpDownToLoad;
  369.             bool success = int.TryParse(PageUpDownToLoad, out pageUpDownToLoad);
  370.             if (!success) {
  371.                 pageUpDownToLoad = 10;
  372.             }
  373.             for (int i = 0; i < pageUpDownToLoad; i++) {
  374.                 CurrentTab.LoadOlderLogEntry();
  375.             }
  376.             SelectedEntry = CurrentTab.LogEntryCollection[0];
  377.         }
  378.        
  379.         private void LoadNewerLogArrayAction() {
  380.             Log9KUtil.ExecuteCommand(StartRemovingFromTopCommand);
  381.             int pageUpDownToLoad;
  382.             bool success = int.TryParse(PageUpDownToLoad, out pageUpDownToLoad);
  383.             if (!success) {
  384.                 pageUpDownToLoad = 10;
  385.             }
  386.             for (int i = 0; i < pageUpDownToLoad; i++) {
  387.                 CurrentTab.LoadNewerLogEntry();
  388.             }
  389.             SelectedEntry = CurrentTab.LogEntryCollection[CurrentTab.LogEntryCollection.Count - 1];
  390.         }
  391.  
  392.         private void SortLogEntriesCollectionAction() {
  393.             CurrentTab.LogEntryCollection.BubbleSort();
  394.         }
  395.  
  396.         private void StartRemovingFromBottomAction() {
  397.             CurrentTab.LogEntryCollection.StartRemovingFromBottom();
  398.         }
  399.  
  400.         private void StartRemovingFromTopAction() {
  401.             CurrentTab.LogEntryCollection.StartRemovingFromTop();
  402.         }
  403.  
  404.         private void LoadOlderLogAction() {
  405.             for (int i = 0; i < 25; i++) {
  406.                 bool s = CurrentTab.LoadOlderLogEntry();
  407.                 if (s) {
  408.                     break;
  409.                 }
  410.             }
  411.         }
  412.  
  413.         private void LoadNewerLogAction() {
  414.             CurrentTab.LoadNewerLogEntry();
  415.         }
  416.  
  417.         private void ShowLastLogsAction(object parameter) {
  418.             if (!CurrentTab.IsAllTab()) {
  419.                 return;
  420.             }
  421.             bool clearCollection = parameter as bool? ?? false;
  422.             CurrentTab.ShowLastLogs(clearCollection);
  423.         }
  424.  
  425.         private void SelectLogEntryAction(object entry) {
  426.             Log9KEntry log9KEntry = entry as Log9KEntry;
  427.             if (log9KEntry != null) {
  428.                 int d = log9KEntry.GetDuplicationHash();
  429.                 var a = CurrentTab.DuplicationsDictionary[d];
  430. //                SelectedEntry = log9KEntry;
  431.             }
  432.         }
  433.  
  434.         private void ShowOrHideSettingsAction() {
  435.             ShowSettings = !ShowSettings;
  436.         }
  437.  
  438.         #region Copy and save entries
  439.        
  440.         /// <summary>
  441.         /// Copy selected entries to clipboard
  442.         /// </summary>
  443.         /// <param name="o"></param>
  444.         private void CopyEntriesAction(object o) {
  445.             string clipboard = GetStringOfSelectedEntries(o);
  446.             Clipboard.SetText(clipboard);            
  447.         }
  448.        
  449.         /// <summary>
  450.         /// Manual save selected entries to file
  451.         /// </summary>
  452.         /// <param name="o"></param>
  453.         private void SaveEntriesAction(object o) {
  454.             string s = GetStringOfSelectedEntries(o);
  455.             SaveFileDialog sfd = new SaveFileDialog {
  456.                 AddExtension = true,
  457.                 DefaultExt = ".log",
  458.                 CheckPathExists = true,
  459.                 Filter = "Log Files|*.log",
  460.                 InitialDirectory = Path.GetFullPath(Settings.Folder)
  461.             };
  462.             sfd.ShowDialog();
  463.             string filename = sfd.FileName;
  464.             if (filename == "") {
  465.                 return;
  466.             }
  467.             if (File.Exists(filename)) {
  468.                 File.Delete(filename);
  469.             }
  470.             File.WriteAllText(filename, s);
  471.         }
  472.  
  473.         /// <summary>
  474.         /// Get selected entries as one string
  475.         /// </summary>
  476.         /// <param name="o"></param>
  477.         /// <returns></returns>
  478.         private string GetStringOfSelectedEntries(object o) {
  479.             string s = "";
  480.             IList selectedEntries = GetSelectedEntries(o);
  481.             foreach (Log9KEntry e in selectedEntries) {
  482.                 s += e + Environment.NewLine;
  483.             }
  484.             return s;
  485.         }
  486.        
  487.         /// <summary>
  488.         /// Checks is given object a list and returns it casted if so
  489.         /// </summary>
  490.         /// <param name="o"></param>
  491.         /// <returns>returns list of selected log entries</returns>
  492.         private IList GetSelectedEntries(object o) {
  493.             if (o == null) {
  494.                 return null;
  495.             }
  496.             IList selectedEntries = o as IList;
  497.  
  498.             if (selectedEntries == null) {
  499.                 return null;
  500.             }
  501.             return selectedEntries;
  502.         }
  503.  
  504.         #endregion
  505.  
  506.  
  507.         #region Time filter
  508.  
  509.         private void CancelFilterByTimeAction() {
  510.             if (CurrentTab.IsAllTab()) {
  511.                 CurrentTab.ShowLastLogs(true);
  512.             } else {
  513.                 LogEntriesCollectionView.Filter = null;
  514.             }
  515.         }
  516.  
  517.         /// <summary>
  518.         /// Set time filter for collection view
  519.         /// </summary>
  520.         private void FilterByTimeAction(object timeEnd) {
  521.             bool a = false;
  522.             if (timeEnd != null) {
  523.                 a = timeEnd is string;
  524.             }
  525.  
  526.             if (!a) {
  527.                 _timeEndSuccess = DateTime.TryParse(FilterTimeEnd, out _endDateTime);
  528.             } else {
  529.                 _timeEndSuccess = DateTime.TryParse((string)timeEnd, out _endDateTime);
  530.             }
  531.  
  532.             _timeStartSuccess = DateTime.TryParse(FilterTimeStart, out _startDateTime);
  533.             if (!(_timeStartSuccess || _timeEndSuccess)) {
  534.                 LogEntriesCollectionView.Filter = null;
  535.                 return;
  536.             }
  537.             if (CurrentTab.IsAllTab()) {
  538.                 CurrentTab.FilterByTime(_startDateTime, _endDateTime);
  539.             } else {
  540.                 LogEntriesCollectionView.Filter = Filter;    
  541.             }
  542.         }
  543.        
  544.  
  545.         /// <summary>
  546.         /// Time filter for collection view
  547.         /// </summary>
  548.         /// <param name="o"></param>
  549.         /// <returns></returns>
  550.         private bool Filter(object o) {
  551.             if (!(o is Log9KEntry)) {
  552.                 return false;
  553.             }
  554.             Log9KEntry entry = (Log9KEntry) o;
  555.             DateTime entryTime= entry.Time.ToDateTime();
  556.             if (!_timeStartSuccess) {
  557.                 if (entryTime.CompareTo(_endDateTime) <= 0) {
  558.                     return true;
  559.                 }
  560.             }
  561.             if (!_timeEndSuccess) {
  562.                 if (entryTime.CompareTo(_startDateTime) >= 0) {
  563.                     return true;
  564.                 }
  565.             }
  566.             if (entryTime.CompareTo(_startDateTime) >= 0 && (entryTime.CompareTo(_endDateTime) <= 0)) {
  567.                 return true;
  568.             }
  569.             return false;
  570.         }
  571.  
  572.         #endregion
  573.  
  574.  
  575.         #endregion
  576.  
  577.  
  578.     }
  579. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement