Last-Modified: Sun, 02 Aug 2026 07:09:16 GMT Expires: Wed, 30 Jul 2036 07:09:16 GMT TouchVirtualizedContentControl.cs « Controls « Tango.Touch « Visual_Studio « Software - Tango - Twine softwares
aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Touch/Controls/TouchVirtualizedContentControl.cs
blob: 72bb7dd0654d0081e67b62f3d5e71835f4e6a507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using Tango.Core.Commands;
using Tango.FSE.BL.WindowsRegistry;
using Tango.FSE.Common.Helpers;
using Tango.FSE.Web.Messages;
using Tango.Integration.Logging;
using Tango.Logging;
using ZetaIpc.Runtime.Server;
using Tango.TFS;
using System.Diagnostics;
using Tango.Settings;

namespace Tango.FSE.LogViewer.UI.ViewModels
{
    public class LayoutViewVM : LogViewerViewModel
    {
        private ICollectionView _view;
        private IpcServer _ipcServer;

        private LogViewerSettings Settings { get; set; }

        public ObservableCollection<LogFileTabViewVM> LogFiles { get; set; }

        private LogFileTabViewVM _selectedLogFile;
        public LogFileTabViewVM SelectedLogFile
        {
            get { return _selectedLogFile; }
            set { _selectedLogFile = value; RaisePropertyChangedAuto(); OnSelectedLogFileChanged(); }
        }

        private String _filter;
        public String Filter
        {
            get { return _filter; }
            set { _filter = value; RaisePropertyChangedAuto(); OnFilterChanged(); }
        }

        private bool _wrapLines;
        public bool WrapLines
        {
            get { return _wrapLines; }
            set { _wrapLines = value; RaisePropertyChangedAuto(); OnWrapLinesChanged(); }
        }

        private List<String> _recentLogFiles;
        public List<String> RecentLogFiles
        {
            get { return _recentLogFiles; }
            set { _recentLogFiles = value; RaisePropertyChangedAuto(); }
        }

        private bool _isTabsListOpened;
        public bool IsTabsListOpened
        {
            get { return _isTabsListOpened; }
            set { _isTabsListOpened = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand OpenLogFileCommand { get; set; }
        public RelayCommand<String> OpenRecentLogFileCommand { get; set; }
        public RelayCommand<LogFileTabViewVM> CloseLogFileCommand { get; set; }
        public RelayCommand SaveAsLogFileCommand { get; set; }
        public RelayCommand ExitCommand { get; set; }

        public LayoutViewVM()
        {
            WrapLines = true;
            RecentLogFiles = new List<string>();
            LogFiles = new ObservableCollection<LogFileTabViewVM>();
            OpenLogFileCommand = new RelayCommand(OpenLogFiles);
            CloseLogFileCommand = new RelayCommand<LogFileTabViewVM>(CloseLogFile);
            SaveAsLogFileCommand = new RelayCommand(SaveAsLogFile);
            ExitCommand = new RelayCommand(ExitApplication);
            OpenRecentLogFileCommand = new RelayCommand<string>(OpenRecentLogFile);

            _ipcServer = new IpcServer();
            Application.Current.MainWindow.ContentRendered += MainWindow_ContentRendered;
        }

        private void MainWindow_ContentRendered(object sender, EventArgs e)
        {
            var startupArgs = Environment.GetCommandLineArgs().Skip(1).ToList();
            OnApplicationReady(startupArgs);
        }

        private void ExitApplication()
        {
            Environment.Exit(0);
        }

        private async void SaveAsLogFile()
        {
            if (SelectedLogFile != null)
            {
                var result = await StorageProvider.SaveFile("Save Log File As...", "Twine Log Files|*.log", SelectedLogFile.Name, ".log");
                if (result.Confirmed)
                {
                    try
                    {
                        await Task.Factory.StartNew(() =>
                        {
                            File.Copy(SelectedLogFile.File, result.SelectedItem, true);
                        });

                        await NotificationProvider.ShowSuccess("Log file saved successfully.");
                    }
                    catch (Exception ex)
                    {
                        await NotificationProvider.ShowError($"Error saving log file.\n{ex.FlattenMessage()}");
                    }
                }
            }
        }

        private async void OpenLogFiles()
        {
            var result = await StorageProvider.OpenFiles("Browse for log files", "Twine Log Files|*.log");
            if (result.Confirmed)
            {
                await OpenLogFiles(result.SelectedItems);
            }
        }

        private Task OpenLogFiles(String file)
        {
            return OpenLogFiles(new List<String>() { file });
        }

        private async Task OpenLogFiles(List<String> files)
        {
            using (var task = NotificationProvider.PushTaskItem($"Loading '{Path.GetFileName(files.FirstOrDefault())}'..."))
            {
                bool setLastSelected = true;

                foreach (var file in files)
                {
                    task.UpdateProgress($"Loading '{Path.GetFileName(file)}'...");

                    var existingLogFile = LogFiles.ToList().FirstOrDefault(x => x.File == file);

                    if (existingLogFile != null)
                    {
                        setLastSelected = false;
                        SelectedLogFile = existingLogFile;
                        continue;
                    }

                    if (!File.Exists(file))
                    {
                        await NotificationProvider.ShowError($"Error loading log file '{Path.GetFileName(file)}'. The file could not be found.");
                        continue;
                    }

                    List<LogItemBase> logs = new List<LogItemBase>();
                    LogFileTabViewVM logFile = new LogFileTabViewVM();

                    try
                    {
                        DateTime createdTime = File.GetCreationTime(file);

                        await Task.Delay(500);

                        await Task.Factory.StartNew(() =>
                        {
                            if (!Path.GetFileName(file).StartsWith("Embedded"))
                            {
                                ApplicationLogFileParser parser = new ApplicationLogFileParser();
                                logs = parser.Parse(file, createdTime);
                            }
                            else
                            {
                                EmbeddedLogFileParser parser = new EmbeddedLogFileParser();
                                logs = parser.Parse(file, createdTime).Cast<LogItemBase>().ToList();
                                logFile.IsEmbedded = true;
                            }

                            logFile.Name = Path.GetFileName(file);
                            logFile.File = file;
                            logFile.Size = Core.Helpers.FileHelper.GetFriendlyFileSize(new FileInfo(file).Length);
                            logFile.StartTime = createdTime;
                            logFile.EndTime = logs.Last().TimeStamp;
                        });

                        logFile.Logs = new ObservableCollection<LogItemBase>(logs);
                        LogFiles.Add(logFile);

                        Settings.RecentFiles.RemoveAll(x => x == logFile.File);
                        Settings.RecentFiles.Insert(0, logFile.File);
                        if (Settings.RecentFiles.Count > 10)
                        {
                            Settings.RecentFiles.Remove(Settings.RecentFiles.Last());
                        }
                        Settings.Save();
                        RecentLogFiles = Settings.RecentFiles.ToList();

                    }
                    catch (Exception ex)
                    {
                        await NotificationProvider.ShowError($"Error loading log file '{Path.GetFileName(file)}'.\n{ex.FlattenMessage()}");
                    }
                }

                if (setLastSelected)
                {
                    SelectedLogFile = LogFiles.LastOrDefault();
                }
            }
        }

        private void CloseLogFile(LogFileTabViewVM logFile)
        {
            int index = LogFiles.IndexOf(logFile);
            int count = LogFiles.Count;
            LogFileTabViewVM selectedLogFile = SelectedLogFile;
            LogFiles.Remove(logFile);

            if (selectedLogFile == logFile)
            {
                if (LogFiles.Count > index)
                {
                    SelectedLogFile = LogFiles[index];
                }
                else if (LogFiles.Count > 0)
                {
                    SelectedLogFile = LogFiles[index - 1];
                }
                else
                {
                    SelectedLogFile = null;
                }
            }
        }

        private void OnSelectedLogFileChanged()
        {
            IsTabsListOpened = false;

            if (_view != null)
            {
                _view.Filter = null;
                _view.Refresh();
            }

            if (SelectedLogFile != null)
            {
                _view = CollectionViewSource.GetDefaultView(SelectedLogFile.Logs);
                _view.Filter = FilterLogs;
            }
        }

        private void OnFilterChanged()
        {
            _view?.Refresh();
        }

        private bool FilterLogs(object obj)
        {
            LogItemBase log = obj as LogItemBase;
            if (String.IsNullOrWhiteSpace(Filter)) return true;

            if (Filter.Contains("|"))
            {
                List<String> keywords = Filter.Split('|').ToList();

                if (keywords.Any(x => log.Category.ToString().ToLower() == x.ToLower()))
                {
                    return true;
                }
            }
            else
            {
                if (log.Category.ToString().ToLower() == Filter.ToLower())
                {
                    return true;
                }
            }

            if (log.TimeStamp.ToString("HH:mm:ss").Contains(Filter))
            {
                return true;
            }

            if (log.Message.ToLower().Contains(Filter.ToLower()))
            {
                return true;
            }

            return false;
        }

        private void OnApplicationReady(List<string> args)
        {
            Settings = SettingsManager.Default.GetOrCreate<LogViewerSettings>();
            WrapLines = Settings.WrapLines;
            RecentLogFiles = Settings.RecentFiles.ToList();

            try
            {
                LogManager.Log("Starting file association IPC service...");
                _ipcServer.Start(App.FILE_ASSPCIATION_PORT);
                _ipcServer.ReceivedRequest += _ipcServer_ReceivedRequest;
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error starting file association IPC service. Another instance of the application might already be running...");
            }

            HandleStartupArgs(args);
        }

        private void _ipcServer_ReceivedRequest(object sender, ReceivedRequestEventArgs e)
        {
            try
            {
                e.Handled = true;
                var argsObject = JsonConvert.DeserializeObject<ArgsObject>(e.Request);

                InvokeUI(() =>
                {
                    if (Application.Current.MainWindow.WindowState == WindowState.Minimized)
                    {
                        Application.Current.MainWindow.WindowState = WindowState.Normal;
                    }
                    Application.Current.MainWindow.Activate();
                    HandleStartupArgs(argsObject.Args);
                });
            }
            catch (Exception ex)
            {
                LogManager.Log(ex);
            }
        }

        private async void HandleStartupArgs(List<string> args)
        {
            if (args == null) return;

            args = StartupArgsHelper.CleanArgsFromWeb(args.ToArray()).ToList();

            if (args.Count == 3 && args[0] == "-remote")
            {
                try
                {
                    LoadFromTFS(args[2], (LoadFromTfsMode)Enum.Parse(typeof(LoadFromTfsMode), args[1]));
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex);
                    await NotificationProvider.ShowError($"Error loading the specified work item logs.\n{ex.FlattenMessage()}");
                }
            }
            else
            {
                await OpenLogFiles(args);
            }
        }

        private async void LoadFromTFS(String workItemID, LoadFromTfsMode mode)
        {
            try
            {
                String tempFile = null;

                using (NotificationProvider.PushTaskItem($"Loading {mode} logs from work item {workItemID}..."))
                {
                    await Task.Factory.StartNew(() =>
                    {
                        int id = int.Parse(workItemID);

                        var encrypted = RegistryService.Default.GetValue("TFS");
                        BugReportingInfoResponse info = CryptographyProvider.Decrypt<BugReportingInfoResponse>(encrypted);

                        TeamFoundationServiceClient client = new TeamFoundationServiceClient(info.CollectionUrl, String.Empty, info.PersonalToken);
                        Project project = client.GetProject("Tango").Result;

                        var attachements = client.GetWorkItemAttachements(id).Result;
                        AttachementHandler handler = attachements.Where(x => x.Extension == ".log").LastOrDefault(x => x.Name.StartsWith(mode.ToDescription()) && !x.Name.ToLower().Contains("session"));

                        var tempFolder = TemporaryManager.CreateFolder();
                        tempFile = Path.Combine(tempFolder, handler.Name);
                        handler.Download(tempFile).Wait();
                    });

                    await OpenLogFiles(tempFile);
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex);
                await NotificationProvider.ShowError($"Error loading the specified work item logs.\n{ex.FlattenMessage()}");
            }
        }

        internal async void OnFilesDropped(List<string> files)
        {
            await OpenLogFiles(files.Where(x => File.Exists(x) && Path.GetExtension(x).ToLower() == ".log").ToList());
        }

        private void OnWrapLinesChanged()
        {
            if (Settings != null)
            {
                Settings.WrapLines = WrapLines;
                Settings.Save();
            }
        }

        private async void OpenRecentLogFile(string file)
        {
            await OpenLogFiles(file);
        }
    }
}