public void RemoveActionFromMenu(Guid actionGuid)
        {
            var found = PluginMenuItems.FirstOrDefault(it => it.Action?.Item.Guid == actionGuid);

            if (found != null)
            {
                PluginMenuItems.Remove(found);
            }
        }
        public void AddActionToMenu(PluginPart <IAdditionalAction> action)
        {
            var item =
                new PluginMenuItemModel(
                    action.Item.GetActionTitle(),
                    PluginItemCommand,
                    action: action
                    );

            PluginMenuItems.Add(item);
        }
Beispiel #3
0
        public MainWindowViewModel()
        {
            var tmpCurDir = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

            try
            {
                GuiActionsManager.SetStatusBarMessage = (string text) =>
                {
                    StatusBarVM.StatusText = text;
                };

                MessengerInstance.Register(this, new Action <Messaging.Requests.PrepareForGenerationRequest>(OnPrepareForGenerationRequest));
                MessengerInstance.Register(this, new Action <ControllerCodeGeneratedMessage>(OnControllerCodeGenerated));
                MessengerInstance.Register(this, new Action <ControllerProjectGeneratedMessage>(OnControllerProjectGenerated));
                MessengerInstance.Register(this, new Action <ControllerFileNameChangedMessage>(OnControllerFileNameChanged));

                // Load application settings and defaults
                TLCGenSplashScreenHelper.ShowText("Laden instellingen en defaults...");
                SettingsProvider.Default.LoadApplicationSettings();
                DefaultsProvider.Default.LoadSettings();
                TemplatesProvider.Default.LoadSettings();

                TLCGenModelManager.Default.InjectDefaultAction((x, s) => DefaultsProvider.Default.SetDefaultsOnModel(x, s));
                TLCGenControllerDataProvider.Default.InjectDefaultAction(x => DefaultsProvider.Default.SetDefaultsOnModel(x));

                // Load available applicationparts and plugins
                var assms = Assembly.GetExecutingAssembly();
                var types = from t in assms.GetTypes()
                            where t.IsClass && t.Namespace == "TLCGen.ViewModels"
                            select t;
                TLCGenSplashScreenHelper.ShowText("Laden applicatie onderdelen...");
                TLCGenPluginManager.Default.LoadApplicationParts(types.ToList());
                TLCGenSplashScreenHelper.ShowText("Laden plugins...");
                TLCGenPluginManager.Default.LoadPlugins(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins\\"));

                // Instantiate all parts
                _ApplicationParts = new List <Tuple <TLCGenPluginElems, ITLCGenPlugin> >();
                var parts = TLCGenPluginManager.Default.ApplicationParts.Concat(TLCGenPluginManager.Default.ApplicationPlugins);
                foreach (var part in parts)
                {
                    ITLCGenPlugin instpl = part.Item2;
                    TLCGenSplashScreenHelper.ShowText($"Laden plugin {instpl.GetPluginName()}...");
                    var flags = Enum.GetValues(typeof(TLCGenPluginElems));
                    foreach (TLCGenPluginElems elem in flags)
                    {
                        if ((part.Item1 & elem) == elem)
                        {
                            switch (elem)
                            {
                            case TLCGenPluginElems.Generator:
                                Generators.Add(new IGeneratorViewModel(instpl as ITLCGenGenerator));
                                break;

                            case TLCGenPluginElems.HasSettings:
                                ((ITLCGenHasSettings)instpl).LoadSettings();
                                break;

                            case TLCGenPluginElems.Importer:
                                MenuItem mi = new MenuItem();
                                mi.Header           = instpl.GetPluginName();
                                mi.Command          = ImportControllerCommand;
                                mi.CommandParameter = instpl;
                                ImportMenuItems.Add(mi);
                                break;

                            case TLCGenPluginElems.IOElementProvider:
                                break;

                            case TLCGenPluginElems.MenuControl:
                                PluginMenuItems.Add(((ITLCGenMenuItem)instpl).Menu);
                                break;

                            case TLCGenPluginElems.TabControl:
                                break;

                            case TLCGenPluginElems.ToolBarControl:
                                break;

                            case TLCGenPluginElems.XMLNodeWriter:
                                break;

                            case TLCGenPluginElems.PlugMessaging:
                                (instpl as ITLCGenPlugMessaging).UpdateTLCGenMessaging();
                                break;

                            case TLCGenPluginElems.Switcher:
                                (instpl as ITLCGenSwitcher).ControllerSet += (sender, model) => { SetController(model); };
                                (instpl as ITLCGenSwitcher).FileNameSet   += (sender, model) =>
                                {
                                    TLCGenControllerDataProvider.Default.ControllerFileName = model;
                                };
                                break;
                            }
                        }
                        TLCGenPluginManager.LoadAddinSettings(instpl, part.Item2.GetType(), SettingsProvider.Default.Settings.CustomData);
                    }
                    _ApplicationParts.Add(new Tuple <TLCGenPluginElems, ITLCGenPlugin>(part.Item1, instpl as ITLCGenPlugin));
                }
                if (Generators.Count > 0)
                {
                    SelectedGenerator = Generators[0];
                }

                // Construct the ViewModel
                ControllerVM = new ControllerViewModel();

                if (!DesignMode.IsInDesignMode)
                {
                    if (Application.Current != null && Application.Current.MainWindow != null)
                    {
                        Application.Current.MainWindow.Closing += new CancelEventHandler(MainWindow_Closing);
                    }
                }

#if !DEBUG
                Application.Current.DispatcherUnhandledException += (o, e) =>
                {
                    string message = "Er is een onverwachte fout opgetreden.\n\n";
                    if (TLCGenControllerDataProvider.Default.Controller != null)
                    {
                        try
                        {
                            string t = TLCGenControllerDataProvider.Default.ControllerFileName;
                            if (string.IsNullOrWhiteSpace(TLCGenControllerDataProvider.Default.ControllerFileName))
                            {
                                TLCGenControllerDataProvider.Default.ControllerFileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TLC_recoverysave.tlc");
                            }
                            TLCGenControllerDataProvider.Default.ControllerFileName =
                                System.IO.Path.Combine(
                                    System.IO.Path.GetDirectoryName(TLCGenControllerDataProvider.Default.ControllerFileName),
                                    DateTime.Now.ToString("yyyyMMdd-HHmmss-", System.Globalization.CultureInfo.InvariantCulture) +
                                    System.IO.Path.GetFileName(TLCGenControllerDataProvider.Default.ControllerFileName));
                            TLCGenControllerDataProvider.Default.SaveController();
                            message += "De huidige regeling is hier opgeslagen:\n" + TLCGenControllerDataProvider.Default.ControllerFileName + "\n\n";
                            if (t != null)
                            {
                                TLCGenControllerDataProvider.Default.ControllerFileName = t;
                            }
                        }
                        catch
                        {
                        }
                    }
                    message += "Gelieve dit probleem inclusief onderstaande details doorgeven aan de ontwikkelaar:\n\n";
                    var win = new TLCGen.Dialogs.UnhandledExceptionWindow();
                    win.DialogTitle          = "Onverwachte fout in TLCGen";
                    win.DialogMessage        = message;
                    win.DialogExpceptionText = e.Exception.ToString();
                    win.ShowDialog();
                };
#endif
            }
            catch (Exception e)
            {
                TLCGenSplashScreenHelper.Hide();

                string message = "Er is een onverwachte fout opgetreden.\n\n";
                message += "Gelieve dit probleem inclusief onderstaande details doorgeven aan de ontwikkelaar:\n\n";
                var win = new UnhandledExceptionWindow
                {
                    DialogTitle          = "Onverwachte fout in TLCGen",
                    DialogMessage        = message,
                    DialogExpceptionText = e.ToString()
                };
                win.ShowDialog();
            }

            Directory.SetCurrentDirectory(tmpCurDir);

#if !DEBUG
            // Find out if there is a newer version available via Wordpress REST API
            Task.Run(() =>
            {
                // clean potential old data
                var key      = Registry.CurrentUser.OpenSubKey("Software", true);
                var sk1      = key?.OpenSubKey("CodingConnected e.U.", true);
                var sk2      = sk1?.OpenSubKey("TLCGen", true);
                var tempFile = (string)sk2?.GetValue("TempInstallFile", null);
                if (tempFile != null)
                {
                    if (File.Exists(tempFile))
                    {
                        File.Delete(tempFile);
                    }
                    sk2?.DeleteValue("TempInstallFile");
                }

                var webRequest = WebRequest.Create(@"https://codingconnected.eu/wp-json/wp/v2/pages/1105");
                webRequest.UseDefaultCredentials = true;
                using (var response = webRequest.GetResponse())
                    using (var content = response.GetResponseStream())
                        if (content != null)
                        {
                            using (var reader = new StreamReader(content))
                            {
                                var strContent       = reader.ReadToEnd().Replace("\n", "");
                                var jsonDeserializer = new JavaScriptSerializer();
                                var deserializedJson = jsonDeserializer.Deserialize <dynamic>(strContent);
                                if (deserializedJson == null)
                                {
                                    return;
                                }
                                var contentData = deserializedJson["content"];
                                if (contentData == null)
                                {
                                    return;
                                }
                                var renderedData = contentData["rendered"];
                                if (renderedData == null)
                                {
                                    return;
                                }
                                var data = renderedData as string;
                                if (data == null)
                                {
                                    return;
                                }
                                var all       = data.Split('\r');
                                var tlcgenVer = all.FirstOrDefault(v => v.StartsWith("TLCGen="));
                                if (tlcgenVer == null)
                                {
                                    return;
                                }
                                var oldvers = Assembly.GetEntryAssembly().GetName().Version.ToString().Split('.');
                                var newvers = tlcgenVer.Replace("TLCGen=", "").Split('.');
                                bool newer  = false;
                                if (oldvers.Length > 0 && oldvers.Length == newvers.Length)
                                {
                                    for (int i = 0; i < newvers.Length; i++)
                                    {
                                        var o = int.Parse(oldvers[i]);
                                        var n = int.Parse(newvers[i]);
                                        if (o > n)
                                        {
                                            break;
                                        }
                                        if (n > o)
                                        {
                                            newer = true;
                                            break;
                                        }
                                    }
                                }
                                if (newer)
                                {
                                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                    {
                                        var w = new NewVersionAvailableWindow(tlcgenVer.Replace("TLCGen=", ""));
                                        w.ShowDialog();
                                    });
                                }
                            }
                        }
            });
#endif
        }