예제 #1
0
        internal static LaunchOptions FromContext(LaunchContext context)
        {
            bool   startMinimized = false;
            string startUrl       = null;

            if (context.Arguments.Length == 2 && context.Arguments[1] == "-u")
            {
                return(new LaunchOptions(LaunchOptions.LaunchMode.Unregister, null));
            }
            else if (context.Arguments.Length == 2 && context.Arguments[1] == "-m")
            {
                startMinimized = true;
            }
            else if (context.Arguments.Length == 2 && context.Arguments[1] == "-r")
            {
                return(new LaunchOptions(LaunchOptions.LaunchMode.Register, null));
            }
            else if (context.Arguments.Length == 2)
            {
                startUrl = context.Arguments[1];
            }
            else if (context.Arguments.Length > 3 && context.Arguments[1] == "diff")
            {
                LaunchOptions.DiffToolModeOptions diffOptions = new LaunchOptions.DiffToolModeOptions(context);
                return(new LaunchOptions(LaunchOptions.LaunchMode.DiffTool, diffOptions));
            }

            LaunchOptions.NormalModeOptions options = new LaunchOptions.NormalModeOptions(startMinimized, startUrl);
            return(new LaunchOptions(LaunchOptions.LaunchMode.Normal, options));
        }
예제 #2
0
        private static void onLaunchAnotherInstance(LaunchOptions options, LaunchContext context)
        {
            IntPtr mainWindow = context.GetWindowByCaption(Constants.MainWindowCaption, true);

            if (mainWindow != IntPtr.Zero)
            {
                if (context.Arguments.Length > 1)
                {
                    string message = String.Join("|", context.Arguments);
                    Win32Tools.SendMessageToWindow(mainWindow, message);
                }
                Win32Tools.ForceWindowIntoForeground(mainWindow);
            }
            else
            {
                // This may happen if a custom protocol link is quickly clicked more than once in a row

                Trace.TraceInformation(String.Format("Cannot find Main Window"));

                // bring to front any window
                IntPtr window = context.GetWindowByCaption(String.Empty, true);
                if (window != IntPtr.Zero)
                {
                    Win32Tools.ForceWindowIntoForeground(window);
                }
                else
                {
                    Trace.TraceInformation(String.Format("Cannot find application windows"));
                }
            }
        }
예제 #3
0
        private static string getLogFileName(LaunchContext context)
        {
            bool isMainInstance = context.IsRunningSingleInstance;

            string filenamePrefix = isMainInstance ? "mrhelper.main" : "mrhelper.second.instance";
            string filenameSuffix = isMainInstance ? String.Empty : ".id" + context.CurrentProcess.Id.ToString();
            string filename       = String.Format("{0}.{1}{2}.log", filenamePrefix,
                                                  DateTime.Now.ToString(Constants.TimeStampLogFilenameFormat), filenameSuffix);

            return(Path.Combine(getApplicationDataPath(), filename));
        }
예제 #4
0
        private static void Main()
        {
            try
            {
                Settings = new UserDefinedSettings();
            }
            catch (CorruptedSettingsException ex)
            {
                string message = String.Format(
                    "Cannot launch mrHelper because application configuration file at \"{0}\" could not be loaded. " +
                    "It's probably corrupted. Try deleting the file or contact developers.", ex.ConfigFilePath);
                MessageBox.Show(message, "Fatal error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            using (LaunchContext context = new LaunchContext())
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.ThreadException += (sender, e) => HandleUnhandledException(e.Exception);

                try
                {
                    initializeGitLabSharpLibrary();

                    string currentLogFileName    = getLogFileName(context);
                    CustomTraceListener listener = createTraceListener(currentLogFileName);
                    if (listener == null)
                    {
                        // Cannot do anything good here
                        return;
                    }

                    Directory.SetCurrentDirectory(Path.GetDirectoryName(context.CurrentProcess.MainModule.FileName));
                    ServiceManager = new ServiceManager();
                    createFeedbackReporter(currentLogFileName, listener);

                    launchFromContext(context);
                }
                catch (Exception ex) // Any unhandled exception, including CSE
                {
                    HandleUnhandledException(ex);
                }
            }
        }
예제 #5
0
        private static void onLaunchAnotherInstance(LaunchContext context)
        {
            IntPtr mainWindow = context.GetWindowByCaption(Constants.MainWindowCaption, true);

            if (mainWindow != IntPtr.Zero)
            {
                if (context.Arguments.Length > 1)
                {
                    string message = String.Join("|", context.Arguments.Skip(1)); // skip executable path
                    Win32Tools.SendMessageToWindow(mainWindow, message);
                }
                Win32Tools.SendMessageToWindow(mainWindow, "show");
            }
            else
            {
                // This may happen if a custom protocol link is quickly clicked more than once in a row.
                // In the scope of #453 decided that it is simpler to not handle this case at all.
            }
        }
예제 #6
0
        private static bool checkArguments(LaunchContext context)
        {
            if (context.Arguments.Length == 2)
            {
                if (context.Arguments[1] == "-m" || context.Arguments[1] == "-u")
                {
                    return(true);
                }

                if (!context.Arguments[1].StartsWith(Constants.CustomProtocolName + "://"))
                {
                    string message = String.Format("Unsupported protocol found in URL {0}", context.Arguments[1]);
                    Trace.TraceError(message);
                    MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }
            }
            else if (context.Arguments.Length == 3)
            {
                if (context.Arguments[1] == "-r" && Directory.Exists(context.Arguments[2]))
                {
                    return(true);
                }

                return(false);
            }
            else if (context.Arguments.Length > 3)
            {
                if (context.Arguments[1] == "diff")
                {
                    return(true);
                }

                string arguments = String.Join(" ", context.Arguments);
                Trace.TraceError(String.Format("Invalid arguments {0}", arguments));
                MessageBox.Show("Invalid arguments", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            return(true);
        }
예제 #7
0
        private static void launchFromContext(LaunchContext context)
        {
            LaunchOptions options = LaunchOptions.FromContext(context);

            switch (options.Mode)
            {
            case LaunchOptions.LaunchMode.DiffTool:
                onLaunchFromDiffTool(options);
                break;

            case LaunchOptions.LaunchMode.Register:
                if (registerCustomProtocol())
                {
                    integrateInGitExtensions();
                    integrateInSourceTree();
                }
                break;

            case LaunchOptions.LaunchMode.Unregister:
                unregisterCustomProtocol();
                break;

            case LaunchOptions.LaunchMode.Normal:
                if (context.IsRunningSingleInstance)
                {
                    onLaunchMainInstance(options);
                }
                else
                {
                    onLaunchAnotherInstance(context);
                }
                break;

            default:
                Debug.Assert(false);
                break;
            }
        }
예제 #8
0
        private static void onLaunchFromDiffTool(LaunchOptions launchOptions)
        {
            LaunchContext context = (launchOptions.SpecialOptions as LaunchOptions.DiffToolModeOptions).LaunchContext;

            if (context.IsRunningSingleInstance)
            {
                Trace.TraceWarning("Merge Request Helper is not running");
                MessageBox.Show("Merge Request Helper is not running. Discussion cannot be created", "Warning",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            IntPtr concurrentDiscussionWindow = context.GetWindowByCaption(Constants.StartNewThreadCaption, false);

            if (concurrentDiscussionWindow != IntPtr.Zero)
            {
                Trace.TraceWarning(String.Format("Found a concurrent {0} window", Constants.StartNewThreadCaption));
                Win32Tools.ForceWindowIntoForeground(concurrentDiscussionWindow);
                return;
            }

            int parentToolPID = -1;

            try
            {
                string diffToolName = Path.GetFileNameWithoutExtension(createDiffTool().GetToolCommand());
                StorageSupport.LocalCommitStorageType type = ConfigurationHelper.GetPreferredStorageType(Settings);
                string toolProcessName = type == StorageSupport.LocalCommitStorageType.FileStorage ? diffToolName : "git";
                parentToolPID = getParentProcessId(context.CurrentProcess, toolProcessName);
            }
            catch (ArgumentException ex)
            {
                ExceptionHandlers.Handle("Cannot obtain diff tool file name", ex);
            }
            catch (Win32Exception ex)
            {
                ExceptionHandlers.Handle("Cannot find parent diff tool process", ex);
            }

            if (parentToolPID == -1)
            {
                Trace.TraceError("Cannot find parent diff tool process");
                MessageBox.Show(
                    "Cannot find parent diff tool process. Discussion cannot be created. Is Merge Request Helper running?",
                    "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            string[] argumentsEx = new string[context.Arguments.Length + 1];
            Array.Copy(context.Arguments, 0, argumentsEx, 0, context.Arguments.Length);
            argumentsEx[argumentsEx.Length - 1] = parentToolPID.ToString();

            string message    = String.Join("|", argumentsEx.Skip(1)); // skip executable path
            IntPtr mainWindow = context.GetWindowByCaption(Constants.MainWindowCaption, true);

            if (mainWindow == IntPtr.Zero)
            {
                Debug.Assert(false);
                Trace.TraceWarning("Cannot find Main Window");
                return;
            }

            Win32Tools.SendMessageToWindow(mainWindow, message);
        }
예제 #9
0
 public DiffToolModeOptions(LaunchContext launchContext)
 {
     LaunchContext = launchContext;
 }
예제 #10
0
        private static void Main()
        {
            using (LaunchContext context = new LaunchContext())
            {
                Application.ThreadException += (sender, e) => HandleUnhandledException(e.Exception);

                try
                {
                    string currentLogFileName    = getLogFileName(context);
                    CustomTraceListener listener = null;
                    try
                    {
                        listener = new CustomTraceListener(currentLogFileName,
                                                           String.Format("Merge Request Helper {0} started. PID {1}",
                                                                         Application.ProductVersion, Process.GetCurrentProcess().Id));
                        Trace.Listeners.Add(listener);
                    }
                    catch (ArgumentException)
                    {
                        // Cannot do anything good here
                        return;
                    }

                    Directory.SetCurrentDirectory(Path.GetDirectoryName(context.CurrentProcess.MainModule.FileName));
                    ServiceManager = new ServiceManager();

                    FeedbackReporter = new FeedbackReporter(
                        () =>
                    {
                        listener.Flush();
                        listener.Close();
                        Trace.Listeners.Remove(listener);
                    },
                        () =>
                    {
                        try
                        {
                            listener = new CustomTraceListener(currentLogFileName, null);
                            Trace.Listeners.Add(listener);
                        }
                        catch (Exception)
                        {
                            // Cannot do anything good here
                        }
                    },
                        getApplicationDataPath());

                    LaunchOptions options = LaunchOptions.FromContext(context);
                    switch (options.Mode)
                    {
                    case LaunchOptions.LaunchMode.DiffTool:
                        onLaunchFromDiffTool(options);
                        break;

                    case LaunchOptions.LaunchMode.Register:
                        if (registerCustomProtocol())
                        {
                            integrateInGitUI();
                        }
                        break;

                    case LaunchOptions.LaunchMode.Unregister:
                        unregisterCustomProtocol();
                        break;

                    case LaunchOptions.LaunchMode.Normal:
                        if (context.IsRunningSingleInstance)
                        {
                            onLaunchMainInstace(options);
                        }
                        else
                        {
                            onLaunchAnotherInstance(options, context);
                        }
                        break;

                    default:
                        Debug.Assert(false);
                        break;
                    }
                }
                catch (Exception ex) // whatever unhandled exception
                {
                    HandleUnhandledException(ex);
                }
            }
        }