Exemplo n.º 1
0
        public static bool UpdateDialog(EcasObjectType objType, ComboBox cmbTypes,
                                        DataGridView dgvParams, IEcasObject o, bool bGuiToInternal,
                                        EcasTypeDxMode dxType)
        {
            bool bResult = true;

            try
            {
                if (bGuiToInternal)
                {
                    IEcasParameterized eTypeInfo = null;

                    if (dxType == EcasTypeDxMode.Selection)
                    {
                        string strSel = (cmbTypes.SelectedItem as string);
                        if (!string.IsNullOrEmpty(strSel))
                        {
                            if (objType == EcasObjectType.Event)
                            {
                                eTypeInfo = Program.EcasPool.FindEvent(strSel);
                                o.Type    = eTypeInfo.Type;
                            }
                            else if (objType == EcasObjectType.Condition)
                            {
                                eTypeInfo = Program.EcasPool.FindCondition(strSel);
                                o.Type    = eTypeInfo.Type;
                            }
                            else if (objType == EcasObjectType.Action)
                            {
                                eTypeInfo = Program.EcasPool.FindAction(strSel);
                                o.Type    = eTypeInfo.Type;
                            }
                            else
                            {
                                Debug.Assert(false);
                            }
                        }
                    }
                    else if (dxType == EcasTypeDxMode.ParamsTag)
                    {
                        IEcasParameterized p = (dgvParams.Tag as IEcasParameterized);
                        if ((p != null) && (p.Type != null))
                        {
                            eTypeInfo = p;
                            o.Type    = eTypeInfo.Type;
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }

                    EcasUtil.DataGridViewToParameters(dgvParams, o, eTypeInfo);
                }
                else                 // Internal to GUI
                {
                    if (dxType == EcasTypeDxMode.Selection)
                    {
                        if (o.Type.Equals(PwUuid.Zero))
                        {
                            cmbTypes.SelectedIndex = 0;
                        }
                        else
                        {
                            int i = -1;
                            if (objType == EcasObjectType.Event)
                            {
                                i = cmbTypes.FindString(Program.EcasPool.FindEvent(o.Type).Name);
                            }
                            else if (objType == EcasObjectType.Condition)
                            {
                                i = cmbTypes.FindString(Program.EcasPool.FindCondition(o.Type).Name);
                            }
                            else if (objType == EcasObjectType.Action)
                            {
                                i = cmbTypes.FindString(Program.EcasPool.FindAction(o.Type).Name);
                            }
                            else
                            {
                                Debug.Assert(false);
                            }

                            if (i >= 0)
                            {
                                cmbTypes.SelectedIndex = i;
                            }
                            else
                            {
                                Debug.Assert(false);
                            }
                        }
                    }
                    else
                    {
                        Debug.Assert(dxType != EcasTypeDxMode.ParamsTag);
                    }

                    IEcasParameterized t = null;
                    if (objType == EcasObjectType.Event)
                    {
                        t = Program.EcasPool.FindEvent(cmbTypes.SelectedItem as string);
                    }
                    else if (objType == EcasObjectType.Condition)
                    {
                        t = Program.EcasPool.FindCondition(cmbTypes.SelectedItem as string);
                    }
                    else if (objType == EcasObjectType.Action)
                    {
                        t = Program.EcasPool.FindAction(cmbTypes.SelectedItem as string);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }

                    if (t != null)
                    {
                        EcasUtil.ParametersToDataGridView(dgvParams, t, o);
                    }
                }
            }
            catch (Exception e) { MessageService.ShowWarning(e); bResult = false; }

            return(bResult);
        }
Exemplo n.º 2
0
        private static void RemoveToolBarButton(EcasAction a, EcasContext ctx)
        {
            string strID = EcasUtil.GetParamString(a.Parameters, 0, true);

            Program.MainForm.RemoveCustomToolBarButton(strID);
        }
Exemplo n.º 3
0
        public static void ParametersToDataGridView(DataGridView dg,
                                                    IEcasParameterized p, IEcasObject objDefaults)
        {
            if (dg == null)
            {
                throw new ArgumentNullException("dg");
            }
            if (p == null)
            {
                throw new ArgumentNullException("p");
            }
            if (p.Parameters == null)
            {
                throw new ArgumentException();
            }
            if (objDefaults == null)
            {
                throw new ArgumentNullException("objDefaults");
            }
            if (objDefaults.Parameters == null)
            {
                throw new ArgumentException();
            }

            dg.Rows.Clear();
            dg.Columns.Clear();

            Color clrBack      = dg.DefaultCellStyle.BackColor;
            Color clrValueBack = dg.DefaultCellStyle.BackColor;

            if (clrValueBack.GetBrightness() >= 0.5)
            {
                clrValueBack = UIUtil.DarkenColor(clrValueBack, 0.075);
            }
            else
            {
                clrValueBack = UIUtil.LightenColor(clrValueBack, 0.075);
            }

            dg.ColumnHeadersVisible = false;
            dg.RowHeadersVisible    = false;
            dg.GridColor            = clrBack;
            dg.BackgroundColor      = clrBack;
            dg.DefaultCellStyle.SelectionBackColor = clrBack;
            dg.DefaultCellStyle.SelectionForeColor = dg.DefaultCellStyle.ForeColor;
            dg.AllowDrop                = false;
            dg.AllowUserToAddRows       = false;
            dg.AllowUserToDeleteRows    = false;
            dg.AllowUserToOrderColumns  = false;
            dg.AllowUserToResizeColumns = false;
            dg.AllowUserToResizeRows    = false;
            // dg.EditMode: see below
            dg.Tag = p;

            int nWidth = (dg.ClientSize.Width - UIUtil.GetVScrollBarWidth());

            dg.Columns.Add("Name", KPRes.FieldName);
            dg.Columns.Add("Value", KPRes.FieldValue);
            dg.Columns[0].Width = (nWidth / 2);
            dg.Columns[1].Width = (nWidth / 2);

            bool bUseDefaults = true;

            if (objDefaults.Type == null)
            {
                Debug.Assert(false);
            }                                                                 // Optimistic
            else if (p.Type == null)
            {
                Debug.Assert(false);
            }                                                            // Optimistic
            else if (!objDefaults.Type.Equals(p.Type))
            {
                bUseDefaults = false;
            }

            for (int i = 0; i < p.Parameters.Length; ++i)
            {
                EcasParameter ep = p.Parameters[i];

                dg.Rows.Add();
                DataGridViewRow            row = dg.Rows[dg.Rows.Count - 1];
                DataGridViewCellCollection cc  = row.Cells;

                Debug.Assert(cc.Count == 2);
                cc[0].Value    = ep.Name;
                cc[0].ReadOnly = true;

                string strParam = (bUseDefaults ? EcasUtil.GetParamString(
                                       objDefaults.Parameters, i) : string.Empty);

                DataGridViewCell c = null;
                switch (ep.Type)
                {
                case EcasValueType.String:
                    c       = new DataGridViewTextBoxCell();
                    c.Value = strParam;
                    break;

                case EcasValueType.Bool:
                    c = new DataGridViewCheckBoxCell(false);
                    (c as DataGridViewCheckBoxCell).Value =
                        StrUtil.StringToBool(strParam);
                    break;

                case EcasValueType.EnumStrings:
                    DataGridViewComboBoxCell cmb = new DataGridViewComboBoxCell();
                    cmb.Sorted       = false;
                    cmb.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
                    int iFound = -1;
                    for (int e = 0; e < ep.EnumValues.ItemCount; ++e)
                    {
                        EcasEnumItem eei = ep.EnumValues.Items[e];
                        cmb.Items.Add(eei.Name);
                        if (eei.ID.ToString() == strParam)
                        {
                            iFound = e;
                        }
                    }
                    if (iFound >= 0)
                    {
                        cmb.Value = ep.EnumValues.Items[iFound].Name;
                    }
                    else if (ep.EnumValues.ItemCount > 0)
                    {
                        cmb.Value = ep.EnumValues.Items[0].Name;
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                    c = cmb;
                    break;

                case EcasValueType.Int64:
                    c       = new DataGridViewTextBoxCell();
                    c.Value = FilterTypeI64(strParam);
                    break;

                case EcasValueType.UInt64:
                    c       = new DataGridViewTextBoxCell();
                    c.Value = FilterTypeU64(strParam);
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }

                if (c != null)
                {
                    cc[1] = c;
                }
                cc[1].ReadOnly                 = false;
                cc[1].Style.BackColor          = clrValueBack;
                cc[1].Style.SelectionBackColor = clrValueBack;
            }

            // Perform postponed setting of EditMode (cannot set it earlier
            // due to a Mono bug on FreeBSD);
            // https://sourceforge.net/p/keepass/discussion/329220/thread/cb8270e2/
            dg.EditMode = DataGridViewEditMode.EditOnEnter;
        }
Exemplo n.º 4
0
        private static void ShowEntriesByTag(EcasAction a, EcasContext ctx)
        {
            string strTag = EcasUtil.GetParamString(a.Parameters, 0, true);

            Program.MainForm.ShowEntriesByTag(strTag);
        }
Exemplo n.º 5
0
        private static void ImportIntoCurrentDatabase(EcasAction a, EcasContext ctx)
        {
            PwDatabase pd = Program.MainForm.ActiveDatabase;

            if ((pd == null) || !pd.IsOpen)
            {
                return;
            }

            string strPath = EcasUtil.GetParamString(a.Parameters, 0, true);

            if (string.IsNullOrEmpty(strPath))
            {
                return;
            }
            IOConnectionInfo ioc = IOConnectionInfo.FromPath(strPath);

            string strFormat = EcasUtil.GetParamString(a.Parameters, 1, true);

            if (string.IsNullOrEmpty(strFormat))
            {
                return;
            }
            FileFormatProvider ff = Program.FileFormatPool.Find(strFormat);

            if (ff == null)
            {
                throw new Exception(KPRes.Unknown + ": " + strFormat);
            }

            uint          uMethod = EcasUtil.GetParamUInt(a.Parameters, 2);
            Type          tMM     = Enum.GetUnderlyingType(typeof(PwMergeMethod));
            object        oMethod = Convert.ChangeType(uMethod, tMM);
            PwMergeMethod mm      = PwMergeMethod.None;

            if (Enum.IsDefined(typeof(PwMergeMethod), oMethod))
            {
                mm = (PwMergeMethod)oMethod;
            }
            else
            {
                Debug.Assert(false);
            }
            if (mm == PwMergeMethod.None)
            {
                mm = PwMergeMethod.CreateNewUuids;
            }

            CompositeKey cmpKey = KeyFromParams(a, 3, 4, 5);

            if ((cmpKey == null) && ff.RequiresKey)
            {
                KeyPromptForm kpf = new KeyPromptForm();
                kpf.InitEx(ioc, false, true);

                if (UIUtil.ShowDialogNotValue(kpf, DialogResult.OK))
                {
                    return;
                }

                cmpKey = kpf.CompositeKey;
                UIUtil.DestroyForm(kpf);
            }

            bool?b = true;

            try { b = ImportUtil.Import(pd, ff, ioc, mm, cmpKey); }
            finally
            {
                if (b.GetValueOrDefault(false))
                {
                    Program.MainForm.UpdateUI(false, null, true, null, true, null, true);
                }
            }
        }
        private static void ShowMessageBox(EcasAction a, EcasContext ctx)
        {
            VistaTaskDialog vtd = new VistaTaskDialog();

            string strMain = EcasUtil.GetParamString(a.Parameters, 0, true);

            if (!string.IsNullOrEmpty(strMain))
            {
                vtd.MainInstruction = strMain;
            }

            string strText = EcasUtil.GetParamString(a.Parameters, 1, true);

            if (!string.IsNullOrEmpty(strText))
            {
                vtd.Content = strText;
            }

            uint uIcon = EcasUtil.GetParamUInt(a.Parameters, 2, 0);

            if (uIcon == (uint)MessageBoxIcon.Information)
            {
                vtd.SetIcon(VtdIcon.Information);
            }
            else if (uIcon == (uint)MessageBoxIcon.Question)
            {
                vtd.SetIcon(VtdCustomIcon.Question);
            }
            else if (uIcon == (uint)MessageBoxIcon.Warning)
            {
                vtd.SetIcon(VtdIcon.Warning);
            }
            else if (uIcon == (uint)MessageBoxIcon.Error)
            {
                vtd.SetIcon(VtdIcon.Error);
            }
            else
            {
                Debug.Assert(uIcon == (uint)MessageBoxIcon.None);
            }

            vtd.CommandLinks = false;

            uint uBtns      = EcasUtil.GetParamUInt(a.Parameters, 3, 0);
            bool bCanCancel = false;

            if (uBtns == (uint)MessageBoxButtons.OKCancel)
            {
                vtd.AddButton((int)DialogResult.OK, KPRes.Ok, null);
                vtd.AddButton((int)DialogResult.Cancel, KPRes.Cancel, null);
                bCanCancel = true;
            }
            else if (uBtns == (uint)MessageBoxButtons.YesNo)
            {
                vtd.AddButton((int)DialogResult.OK, KPRes.YesCmd, null);
                vtd.AddButton((int)DialogResult.Cancel, KPRes.NoCmd, null);
                bCanCancel = true;
            }
            else
            {
                vtd.AddButton((int)DialogResult.OK, KPRes.Ok, null);
            }

            uint uDef = EcasUtil.GetParamUInt(a.Parameters, 4, 0);
            ReadOnlyCollection <VtdButton> lButtons = vtd.Buttons;

            if (uDef < (uint)lButtons.Count)
            {
                vtd.DefaultButtonID = lButtons[(int)uDef].ID;
            }

            vtd.WindowTitle = PwDefs.ShortProductName;

            string strTrg = ctx.Trigger.Name;

            if (!string.IsNullOrEmpty(strTrg))
            {
                vtd.FooterText = KPRes.Trigger + @": '" + strTrg + @"'.";
                vtd.SetFooterIcon(VtdIcon.Information);
            }

            int dr;

            if (vtd.ShowDialog())
            {
                dr = vtd.Result;
            }
            else
            {
                string str = (strMain ?? string.Empty);
                if (!string.IsNullOrEmpty(strText))
                {
                    if (str.Length > 0)
                    {
                        str += MessageService.NewParagraph;
                    }
                    str += strText;
                }

                MessageBoxDefaultButton mbdb = MessageBoxDefaultButton.Button1;
                if (uDef == 1)
                {
                    mbdb = MessageBoxDefaultButton.Button2;
                }
                else if (uDef == 2)
                {
                    mbdb = MessageBoxDefaultButton.Button3;
                }

                MessageService.ExternalIncrementMessageCount();
                try
                {
                    dr = (int)MessageService.SafeShowMessageBox(str,
                                                                PwDefs.ShortProductName, (MessageBoxButtons)uBtns,
                                                                (MessageBoxIcon)uIcon, mbdb);
                }
                finally { MessageService.ExternalDecrementMessageCount(); }
            }

            uint uActCondID = EcasUtil.GetParamUInt(a.Parameters, 5, 0);

            bool bDrY = ((dr == (int)DialogResult.OK) ||
                         (dr == (int)DialogResult.Yes));
            bool bDrN = ((dr == (int)DialogResult.Cancel) ||
                         (dr == (int)DialogResult.No));

            bool bPerformAction = (((uActCondID == IdMbcY) && bDrY) ||
                                   ((uActCondID == IdMbcN) && bDrN));

            if (!bPerformAction)
            {
                return;
            }

            uint   uActID         = EcasUtil.GetParamUInt(a.Parameters, 6, 0);
            string strActionParam = EcasUtil.GetParamString(a.Parameters, 7, true);

            if (uActID == IdMbaNone)
            {
            }
            else if (uActID == IdMbaAbort)
            {
                if (bCanCancel)
                {
                    ctx.Cancel = true;
                }
            }
            else if (uActID == IdMbaCmd)
            {
                if (!string.IsNullOrEmpty(strActionParam))
                {
                    WinUtil.OpenUrl(strActionParam, null);
                }
            }
            else
            {
                Debug.Assert(false);
            }
        }
        private static void ExportDatabaseFile(EcasAction a, EcasContext ctx)
        {
            string strPath = EcasUtil.GetParamString(a.Parameters, 0, true);
            // if(string.IsNullOrEmpty(strPath)) return; // Allow no-file exports
            string strFormat = EcasUtil.GetParamString(a.Parameters, 1, true);

            if (string.IsNullOrEmpty(strFormat))
            {
                return;
            }
            string strGroup = EcasUtil.GetParamString(a.Parameters, 2, true);
            string strTag   = EcasUtil.GetParamString(a.Parameters, 3, true);

            PwDatabase pd = Program.MainForm.ActiveDatabase;

            if ((pd == null) || !pd.IsOpen)
            {
                return;
            }

            PwGroup pg = pd.RootGroup;

            if (!string.IsNullOrEmpty(strGroup))
            {
                char    chSep = strGroup[0];
                PwGroup pgSub = pg.FindCreateSubTree(strGroup.Substring(1),
                                                     new char[] { chSep }, false);
                pg = (pgSub ?? (new PwGroup(true, true, KPRes.Group, PwIcon.Folder)));
            }

            if (!string.IsNullOrEmpty(strTag))
            {
                // Do not use pg.Duplicate, because this method
                // creates new UUIDs
                pg = pg.CloneDeep();
                pg.TakeOwnership(true, true, true);

                GroupHandler gh = delegate(PwGroup pgSub)
                {
                    PwObjectList <PwEntry> l = pgSub.Entries;
                    long n = (long)l.UCount;
                    for (long i = n - 1; i >= 0; --i)
                    {
                        if (!l.GetAt((uint)i).HasTag(strTag))
                        {
                            l.RemoveAt((uint)i);
                        }
                    }

                    return(true);
                };

                gh(pg);
                pg.TraverseTree(TraversalMethod.PreOrder, gh, null);
            }

            PwExportInfo     pei = new PwExportInfo(pg, pd, true);
            IOConnectionInfo ioc = (!string.IsNullOrEmpty(strPath) ?
                                    IOConnectionInfo.FromPath(strPath) : null);

            ExportUtil.Export(pei, strFormat, ioc);
        }
        private static void ExecuteShellCmd(EcasAction a, EcasContext ctx)
        {
            string strCmd       = EcasUtil.GetParamString(a.Parameters, 0);
            string strArgs      = EcasUtil.GetParamString(a.Parameters, 1, true, true);
            bool   bWait        = EcasUtil.GetParamBool(a.Parameters, 2);
            uint   uWindowStyle = EcasUtil.GetParamUInt(a.Parameters, 3);
            string strVerb      = EcasUtil.GetParamString(a.Parameters, 4, true);

            if (string.IsNullOrEmpty(strCmd))
            {
                return;
            }

            Process p = null;

            try
            {
                PwEntry pe = null;
                try { pe = Program.MainForm.GetSelectedEntry(false); }
                catch (Exception) { Debug.Assert(false); }

                strCmd = WinUtil.CompileUrl(strCmd, pe, true, null, false);

                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = strCmd;
                if (!string.IsNullOrEmpty(strArgs))
                {
                    psi.Arguments = strArgs;
                }

                bool bShEx = true;
                if (!string.IsNullOrEmpty(strVerb))
                {
                }                                                      // Need ShellExecute
                else if ((uWindowStyle == IdWindowMin) ||
                         (uWindowStyle == IdWindowMax))
                {
                }                                                          // Need ShellExecute
                else
                {
                    string strCmdFlt = strCmd.TrimEnd(new char[] { '\"', '\'',
                                                                   ' ', '\t', '\r', '\n' });
                    if (strCmdFlt.EndsWith(".exe", StrUtil.CaseIgnoreCmp) ||
                        strCmdFlt.EndsWith(".com", StrUtil.CaseIgnoreCmp))
                    {
                        bShEx = false;
                    }
                }
                psi.UseShellExecute = bShEx;

                if (uWindowStyle == IdWindowHidden)
                {
                    psi.CreateNoWindow = true;
                    psi.WindowStyle    = ProcessWindowStyle.Hidden;
                }
                else if (uWindowStyle == IdWindowMin)
                {
                    psi.WindowStyle = ProcessWindowStyle.Minimized;
                }
                else if (uWindowStyle == IdWindowMax)
                {
                    psi.WindowStyle = ProcessWindowStyle.Maximized;
                }

                if (!string.IsNullOrEmpty(strVerb))
                {
                    psi.Verb = strVerb;
                }

                p = NativeLib.StartProcessEx(psi);

                if ((p != null) && bWait)
                {
                    Program.MainForm.UIBlockInteraction(true);
                    MessageService.ExternalIncrementMessageCount();

                    try { p.WaitForExit(); }
                    catch (Exception) { Debug.Assert(false); }

                    MessageService.ExternalDecrementMessageCount();
                    Program.MainForm.UIBlockInteraction(false);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(strCmd + MessageService.NewParagraph + ex.Message);
            }
            finally
            {
                try { if (p != null)
                      {
                          p.Dispose();
                      }
                }
                catch (Exception) { Debug.Assert(false); }
            }
        }