Esempio n. 1
0
        /// <summary>
        /// Imports a wlan connection information to the database. If the entry already exists, then the
        /// parameter behaviour defines the behaviour
        /// </summary>
        /// <param name="wlan">The new wifi connection informations</param>
        /// <param name="pwStorage">The database in which the group lies</param>
        /// <param name="group">The group to which we add the entry</param>
        /// <param name="behaviour">What do we do if the entry already exists?</param>
        /// <param name="slLogger">Where we log to (can be null)</param>
        /// <param name="totalProcents">If we parsed completely, how many (additional) procents of the
        /// total progress did we finish? (Senseless if slLogger = null)</param>
        /// <param name="minProcents">How many procents of the total progress were already finished</param>
        /// <remarks>Note that minProcents + totalProcents has to be less than or equal to 100.</remarks>
        /// <returns>The entry created or null if an error occured</returns>
        public PwEntry ImportTo(WlanProfile wlan, PwDatabase pwStorage, PwGroup group,
                                BehaviourForExEntry behaviour = BehaviourForExEntry.ASK_USER,
                                IStatusLogger slLogger        = null, double totalProcents = 60,
                                double minProcents            = 20)
        {
            slLogger.SetProgress((uint)minProcents);
            PwEntry entry = null;

            String wlanName = wlan.NameOrSSID;// Note that the entries base name must be unprotected!

            if ((wlanName == null) || (wlanName == ""))
            {
                return(null);
            }

            entry = ExistingEntryInGroup(group, wlan);
            if (slLogger != null)
            {
                minProcents += totalProcents / 3.0;
                slLogger.SetProgress((uint)minProcents);
            }

            // Entry exists
            if (entry != null)
            {
                if (behaviour == BehaviourForExEntry.ASK_USER)
                {
                    VistaTaskDialog vtd = new VistaTaskDialog();
                    vtd.CommandLinks = false;
                    //            vtd.Content = strDatabase;
                    vtd.MainInstruction = String.Format("Es ist bereits ein Eintrag namens {0} vorhanden.\n" +
                                                        "Soll der alte Eintrag durch die ausgelesenen Daten ersetzt werden oder " +
                                                        "der Eintrag übersprungen werden?", wlan.name.Value.ReadString());
                    vtd.SetIcon(VtdCustomIcon.Question);
                    //            vtd.VerificationText = KPRes.DialogNoShowAgain;
                    //            vtd.WindowTitle = KeePass.UI. UIExtExt.ProductName;

                    vtd.AddButton((int)BehaviourForExEntry.REPLACE, "Ersetzen", null);
//                        vtd.AddButton((int)BehaviourForExEntry.RENAME_NEW_ONE, "Neuer Eintrag", null);
                    vtd.AddButton((int)BehaviourForExEntry.CANCEL_WITHOUT_ERROR, "Überspringen", null);
                    vtd.ShowDialog();
                    behaviour = (BehaviourForExEntry)vtd.Result;
                }

                switch (behaviour)
                {
                case BehaviourForExEntry.CANCEL_WITHOUT_ERROR:
                    if (slLogger != null)
                    {
                        minProcents += 2.0 * totalProcents / 3.0;
                        slLogger.SetProgress((uint)minProcents);
                    }
                    return(null);

                case BehaviourForExEntry.CANCEL_WITH_ERROR:
                    if (slLogger != null)
                    {
                        slLogger.SetText("Fehler beim Einfügen des Eintrags", LogStatusType.Error);
                        minProcents += 2.0 * totalProcents / 3.0;
                        slLogger.SetProgress((uint)minProcents);
                    }
                    return(null);

                case BehaviourForExEntry.REPLACE:
                    group.Entries.Remove(entry);
                    entry = null;
                    break;

                    /* As long as the title and the ssid in Windows have to identical, we cannot just rename the entry
                     * case BehaviourForExEntry.RENAME_NEW_ONE:*/
                }
            }

            entry = new PwEntry(true, true);
            if (slLogger != null)
            {
                minProcents += totalProcents / 3.0;
                slLogger.SetProgress((uint)minProcents);
            }

            if (wlan.SaveIn(pwStorage, entry))
            {
                group.AddEntry(entry, true);
            }
            else
            {
                entry = null;
            }

            if (slLogger != null)
            {
                minProcents += totalProcents / 3.0;
                slLogger.SetProgress((uint)minProcents);
            }

            // We might have change the name, so let us change it back.
            // Note again that the name must be unprotected!
            wlan.name.Value = new ProtectedString(false, wlanName);
            return(entry);
        }
        /// <summary>Add all entries, we selected to the Windows system.</summary>
        /// <param name="sender">Ignored</param>
        /// <param name="e">Ignored</param>
        public void AddEntriesToXML(object sender, EventArgs e)
        {
            XmlSerializer xml     = new XmlSerializer(typeof(WlanProfile));
            WlanProfile   profile = new WlanProfile();
            StreamWriter  stream;

            // If we only export one entry, then we let the user choose the .xml file otherwise
            // only the folder.
            if (phHost.MainWindow.GetSelectedEntries().Length == 1)
            {
                profile.LoadFrom(phHost.Database, phHost.MainWindow.GetSelectedEntry(true));
                SaveFileDialog fileDialog = new SaveFileDialog();
                fileDialog.Filter = "XML files (*.xml)|*.xml";

                if (fileDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                if ((stream = new StreamWriter(fileDialog.OpenFile())) == null)
                {
                    return;
                }
                xml.Serialize(stream, profile);
                stream.Close();
            }
            else
            {
                FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
                folderBrowser.ShowNewFolderButton = true;
                folderBrowser.Description         = "Verzeichnis für die .xml Dateien wählen";
                if (folderBrowser.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                String path;

                foreach (PwEntry entry in phHost.MainWindow.GetSelectedEntries())
                {
                    profile.LoadFrom(phHost.Database, entry);

                    if (!profile.IsValid)
                    {
                        continue;
                    }

                    path = String.Format("{0}\\{1}.xml", folderBrowser.SelectedPath, profile.NameOrSSID);
                    if (File.Exists(path))
                    {
                        KeePass.UI.VistaTaskDialog vtd = new KeePass.UI.VistaTaskDialog();
                        vtd.CommandLinks    = false;
                        vtd.MainInstruction = String.Format("Es ist bereits eine Datei namens {0} vorhanden.\n" +
                                                            "Soll die Datei ersetzt werden, ein neuer Dateiname erzeugt werden oder der" +
                                                            "Eintrag übersprungen werden?", path);
                        vtd.SetIcon(KeePass.UI.VtdCustomIcon.Question);
                        vtd.WindowTitle = String.Format("Zieldatei für {0}", profile.NameOrSSID);

                        vtd.AddButton((int)MSWifi.BehaviourForExEntry.REPLACE, "Ersetzen", null);
                        vtd.AddButton((int)MSWifi.BehaviourForExEntry.RENAME_NEW_ONE, "Neuer Dateiname", null);
                        vtd.AddButton((int)MSWifi.BehaviourForExEntry.CANCEL_WITHOUT_ERROR, "Überspringen", null);
                        vtd.ShowDialog();
                        switch (vtd.Result)
                        {
                        case (int)MSWifi.BehaviourForExEntry.REPLACE:
                            File.Delete(path);
                            break;

                        case (int)MSWifi.BehaviourForExEntry.RENAME_NEW_ONE:
                            for (int no = 2; File.Exists(path); ++no)
                            {
                                path = String.Format("{0}\\{1} ({2}).xml", folderBrowser.SelectedPath,
                                                     profile.NameOrSSID, no);
                            }
                            break;

                        default:
                        case (int)MSWifi.BehaviourForExEntry.CANCEL_WITHOUT_ERROR:
                            continue;
                        }
                    }

                    stream = new StreamWriter(path, false);
                    xml.Serialize(stream, profile);
                    stream.Close();
                }
            }
        }