/// <summary>
        /// Handles the Click event of the buttonOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks>Documented by Dev03, 2008-12-22</remarks>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            #region NON FEED
            if (!FeedIsVisible)
            {
                foreach (ListViewItem item in listViewLearningModules.SelectedItems)
                {
                    if (item is LearningModuleListViewItem)
                    {
                        lmItem = item as LearningModuleListViewItem;
                        break;
                    }
                }

                if (lmItem != null)
                {
                    try
                    {
                        LearningModulesIndexEntry entry = lmItem.LearningModule;

                        if (!entry.IsVerified || !entry.IsAccessible)
                            return;

                        #region PgSQL
                        if (entry.Connection is PostgreSqlConnectionStringBuilder)
                        {
                            string serverUri = (entry.Connection as PostgreSqlConnectionStringBuilder).SyncURI;
                            string path = Tools.GetFullSyncPath(entry.DisplayName + MLifter.DAL.Helper.SyncedEmbeddedDbExtension,
                                SyncedLearningModulePath, entry.ConnectionName, entry.UserName);
                            bool createNew = (entry.SyncedPath == string.Empty);
                            ConnectionStringStruct css = entry.ConnectionString;

                            switch ((entry.Connection as PostgreSqlConnectionStringBuilder).SyncType)
                            {
                                case SyncType.NotSynchronized:
                                    entry.SyncedPath = string.Empty;
                                    entry.ConnectionString = css;
                                    break;
                                case SyncType.HalfSynchronizedWithDbAccess:
                                    Sync(serverUri, entry.ConnectionString.LmId, entry.UserId, ref path, entry.ConnectionString.ProtectedLm,
                                        entry.ConnectionString.Password, createNew, false, (SyncStatusReportingDelegate)SyncStatusUpdate);
                                    DownloadExtensionFiles(entry, path);

                                    entry.SyncedPath = Tools.GetSyncedPath(path, SyncedLearningModulePath, entry.ConnectionName, entry.UserName);
                                    css.SyncType = SyncType.HalfSynchronizedWithDbAccess;
                                    css.ServerUser = entry.User;
                                    entry.ConnectionString = css;
                                    break;
                                case SyncType.HalfSynchronizedWithoutDbAccess:
                                    Sync(serverUri, entry.ConnectionString.LmId, entry.UserId, ref path, entry.ConnectionString.ProtectedLm,
                                        entry.ConnectionString.Password, createNew, false, (SyncStatusReportingDelegate)SyncStatusUpdate);
                                    DownloadExtensionFiles(entry, path);

                                    entry.SyncedPath = Tools.GetSyncedPath(path, SyncedLearningModulePath, entry.ConnectionName, entry.UserName);
                                    css.SyncType = SyncType.HalfSynchronizedWithoutDbAccess;
                                    css.LearningModuleFolder = (entry.Connection as PostgreSqlConnectionStringBuilder).MediaURI;
                                    entry.ConnectionString = css;
                                    entry.User.Logout();
                                    break;
                                case SyncType.FullSynchronized:
                                    if (entry.User == null)
                                        break;

                                    Sync(serverUri, entry.ConnectionString.LmId, entry.UserId, ref path, entry.ConnectionString.ProtectedLm,
                                        entry.ConnectionString.Password, createNew, false, (SyncStatusReportingDelegate)SyncStatusUpdate);
                                    DownloadMediaContent(entry, path);
                                    DownloadExtensionFiles(entry, path);

                                    entry.SyncedPath = Tools.GetSyncedPath(path, SyncedLearningModulePath, entry.ConnectionName, entry.UserName);
                                    css.SyncType = SyncType.FullSynchronized;
                                    css.LearningModuleFolder = (entry.Connection as PostgreSqlConnectionStringBuilder).MediaURI;
                                    entry.ConnectionString = css;

                                    SyncedModulesIndex.Add(entry.Connection, entry);
                                    SyncedModulesIndex.Dump(Path.Combine(SyncedLearningModulePath, Settings.Default.SyncedModulesFile));

                                    entry.User.Logout();
                                    break;
                                default:
                                    throw new NotImplementedException();
                            }

                            lmIndex.DumpIndexCache(lmIndex.CacheFile);
                        }
                        #endregion
                        #region WEB
                        else if (entry.Connection is WebConnectionStringBuilder)
                        {
                            string serverUri = (entry.Connection as WebConnectionStringBuilder).SyncURI;
                            string path = Tools.GetFullSyncPath(entry.DisplayName + MLifter.DAL.Helper.SyncedEmbeddedDbExtension,
                                SyncedLearningModulePath, entry.ConnectionName, entry.UserName);
                            bool createNew = (entry.SyncedPath == string.Empty);
                            ConnectionStringStruct css = entry.ConnectionString;

                            switch ((entry.Connection as WebConnectionStringBuilder).SyncType)
                            {
                                case SyncType.HalfSynchronizedWithoutDbAccess:
                                    Sync(serverUri, entry.ConnectionString.LmId, entry.UserId, ref path, entry.ConnectionString.ProtectedLm,
                                        entry.ConnectionString.Password, createNew, false, (SyncStatusReportingDelegate)SyncStatusUpdate);
                                    DownloadExtensionFiles(entry, path);

                                    entry.SyncedPath = Tools.GetSyncedPath(path, SyncedLearningModulePath, entry.ConnectionName, entry.UserName);
                                    css.SyncType = SyncType.HalfSynchronizedWithoutDbAccess;
                                    css.ServerUser = entry.User;
                                    css.LearningModuleFolder = (entry.Connection as WebConnectionStringBuilder).MediaURI;
                                    entry.ConnectionString = css;
                                    break;
                                case SyncType.FullSynchronized:
                                    if (entry.User == null)
                                        break;

                                    Sync(serverUri, entry.ConnectionString.LmId, entry.UserId, ref path, entry.ConnectionString.ProtectedLm,
                                        entry.ConnectionString.Password, createNew, false, (SyncStatusReportingDelegate)SyncStatusUpdate);
                                    DownloadMediaContent(entry, path);
                                    DownloadExtensionFiles(entry, path);

                                    entry.SyncedPath = Tools.GetSyncedPath(path, SyncedLearningModulePath, entry.ConnectionName, entry.UserName);
                                    css.SyncType = SyncType.FullSynchronized;
                                    css.ServerUser = entry.User;
                                    css.LearningModuleFolder = (entry.Connection as WebConnectionStringBuilder).MediaURI;
                                    entry.ConnectionString = css;

                                    SyncedModulesIndex.Add(entry.Connection, entry);
                                    SyncedModulesIndex.Dump(Path.Combine(SyncedLearningModulePath, Settings.Default.SyncedModulesFile));
                                    break;
                                case SyncType.NotSynchronized:
                                case SyncType.HalfSynchronizedWithDbAccess:
                                default:
                                    throw new NotImplementedException();
                            }

                            lmIndex.DumpIndexCache(lmIndex.CacheFile);
                        }
                        #endregion

                        OnLearningModuleSelected(new LearningModuleSelectedEventArgs(entry));
                    }
                    catch (NotEnoughtDiskSpaceException)
                    {
                        TaskDialog.MessageBox(Resources.TASK_DIALOG_DISK_FULL_TITLE, Resources.TASK_DIALOG_DISK_FULL_CAPTION,
                            Resources.TASK_DIALOG_DISK_FULL_TEXT, TaskDialogButtons.OK, TaskDialogIcons.Error);
                    }
                    catch (SynchronizationFailedException exp)
                    {
                        Trace.WriteLine(exp.ToString());
                        TaskDialog.MessageBox(Resources.LEARNING_MODULES_PAGE_SYNC_FAILED_TITLE, Resources.LEARNING_MODULES_PAGE_SYNC_FAILED_MAIN,
                            Resources.LEARNING_MODULES_PAGE_SYNC_FAILED_CONTENT, exp.ToString(), string.Empty, string.Empty,
                            TaskDialogButtons.OK, TaskDialogIcons.Error, TaskDialogIcons.Error);
                    }
                }
            }
            #endregion
            else
            {
                if (listViewFeed.SelectedIndices.Count == 0)
                    return;

                ModuleInfo info = (ModuleInfo)listViewFeed.SelectedItems[0].Tag;

                string remoteFile = info.DownloadUrl;
                string folder = LearningModulesIndex.ConnectionsHandler.ConnectionStrings.Find(c => c.IsDefault).ConnectionString;
                string localFile = Path.Combine(folder, Methods.GetValidFileName(info.Title + DAL.Helper.EmbeddedDbExtension, String.Empty));

                if (File.Exists(localFile))
                {
                    EmulatedTaskDialog dialog = new EmulatedTaskDialog();
                    dialog.Owner = ParentForm;
                    dialog.StartPosition = FormStartPosition.CenterParent;
                    dialog.Title = Resources.LEARNING_MODULES_PAGE_DOWNLOAD_EXISTS_CAPTION;
                    dialog.MainInstruction = Resources.LEARNING_MODULES_PAGE_DOWNLOAD_EXISTS_HEADER;
                    dialog.Content = Resources.LEARNING_MODULES_PAGE_DOWNLOAD_EXISTS_TEXT;
                    dialog.CommandButtons = String.Format("{0}|{1}|{2}", Resources.LEARNING_MODULES_PAGE_DOWNLOAD_EXISTS_OPEN,
                        Resources.LEARNING_MODULES_PAGE_DOWNLOAD_EXISTS_OVERWRITE, Resources.LEARNING_MODULES_PAGE_DOWNLOAD_EXISTS_RENAME);
                    dialog.Buttons = TaskDialogButtons.Cancel;
                    dialog.MainIcon = TaskDialogIcons.Question;
                    dialog.MainImages = new Image[] { Resources.document_open_big, Resources.document_save, Resources.document_save_as };
                    dialog.HoverImages = new Image[] { Resources.document_open_big, Resources.document_save, Resources.document_save_as };
                    dialog.CenterImages = true;
                    dialog.BuildForm();
                    if (dialog.ShowDialog() == DialogResult.Cancel)
                        return;

                    switch (dialog.CommandButtonClickedIndex)
                    {
                        case 0:
                            LearningModulesIndexEntry entry = new LearningModulesIndexEntry(new ConnectionStringStruct(DatabaseType.MsSqlCe, localFile));
                            OnLearningModuleSelected(new LearningModuleSelectedEventArgs(entry));
                            return;
                        case 1:
                            lmIndex.EndLearningModulesScan();
                            disposeAndDisconnectLMs();
                            File.Delete(localFile);
                            break;
                        case 2:
                        default:
                            int cnt = 1;
                            while (File.Exists(localFile))
                                localFile = Path.Combine(folder, Methods.GetValidFileName(info.Title + "_" + cnt++ + DAL.Helper.EmbeddedDbExtension, String.Empty));
                            break;
                    }

                }

                info.DownloadUrl = localFile;
                listViewFeed.SelectedItems[0].Tag = info;

                WebClient webClient = new WebClient();
                webClient.DownloadProgressChanged += (s, args) =>
                {
                    UpdateStatusMessage(String.Format(Resources.LEARNING_MODULES_PAGE_DOWNLOAD_PROGRESS,
                        Methods.GetFileSize(args.BytesReceived), Methods.GetFileSize(args.TotalBytesToReceive)), args.ProgressPercentage);
                };
                webClient.DownloadFileCompleted += (s, args) =>
                {
                    HideStatusMessage();
                    OnLearningModuleSelected(new LearningModuleSelectedEventArgs(new LearningModulesIndexEntry(
                        new ConnectionStringStruct(DatabaseType.MsSqlCe, localFile))));
                };
                ShowStatusMessage(message: Resources.LEARNING_MODULES_PAGE_DOWNLOAD_START);
                webClient.DownloadFileAsync(new Uri(remoteFile), localFile);
            }
        }
        private void SetLearningModuleListViewItemGroup(LearningModuleListViewItem item, ItemOrderType order)
        {
            item.Group = null;
            switch (order)
            {
                case ItemOrderType.Location:
                    foreach (ListViewGroup group in listViewLearningModules.Groups)
                    {
                        if (group.Header == item.LearningModule.ConnectionName)
                        {
                            item.Group = group;
                            break;
                        }
                    }

                    if (item.Group == null)
                    {
                        ListViewGroup grp = new ListViewGroup(item.LearningModule.ConnectionName);
                        listViewLearningModules.Groups.Add(grp);
                        item.Group = grp;
                    }
                    break;
                case ItemOrderType.Category:
                    foreach (ListViewGroup group in listViewLearningModules.Groups)
                    {
                        if (group.Header == (item.LearningModule.Category != null ? item.LearningModule.Category.ToString() : Resources.NO_CATEGORY))
                        {
                            item.Group = group;
                            break;
                        }
                    }

                    if (item.Group == null)
                    {
                        ListViewGroup grp = new ListViewGroup((item.LearningModule.Category != null ? item.LearningModule.Category.ToString() : Resources.NO_CATEGORY));
                        listViewLearningModules.Groups.Add(grp);
                        item.Group = grp;
                    }
                    break;
                case ItemOrderType.Author:
                    foreach (ListViewGroup group in listViewLearningModules.Groups)
                    {
                        if (group.Header == (item.LearningModule.Author != string.Empty ? item.LearningModule.Author : Resources.NO_AUTHOR))
                        {
                            item.Group = group;
                            break;
                        }
                    }

                    if (item.Group == null)
                    {
                        ListViewGroup grp = new ListViewGroup((item.LearningModule.Author != string.Empty ? item.LearningModule.Author : Resources.NO_AUTHOR));
                        listViewLearningModules.Groups.Add(grp);
                        item.Group = grp;
                    }
                    break;
                default:
                    throw new ArgumentException();
            }
        }
        /// <summary>
        /// Loads the list into the ListView.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="groupsOnly">if set to <c>true</c> [groups only].</param>
        /// <remarks>Documented by Dev05, 2008-12-10</remarks>
        private void LoadLearningModulesList(List<IIndexEntry> items, bool groupsOnly)
        {
            listViewLearningModules.BeginUpdate();

            if (!groupsOnly)
            {
                listViewLearningModules.Items.Clear();
                foreach (FolderIndexEntry entry in items.FindAll(en => en is FolderIndexEntry))
                {
                    FolderListViewItem item = new FolderListViewItem(entry);
                    listViewLearningModules.Items.Add(item);
                    item.UpdateImage();
                    item.UpdateDetails();
                }
                foreach (LearningModulesIndexEntry entry in items.FindAll(en => en is LearningModulesIndexEntry))
                {
                    LearningModuleListViewItem item = new LearningModuleListViewItem(entry);
                    listViewLearningModules.Items.Add(item);
                    item.UpdateDetails();
                }

                if (listViewLearningModules.Items.Count > 0 && !FeedIsVisible)
                    listViewLearningModules.SelectedIndices.Add(0);
            }

            listViewLearningModules.Groups.Clear();
            foreach (ListViewItem item in listViewLearningModules.Items)
            {
                if (item is LearningModuleListViewItem)
                    SetLearningModuleListViewItemGroup(item as LearningModuleListViewItem);
                else
                    SetFolderListViewItemGroup(item as FolderListViewItem);
            }

            listViewLearningModules.EndUpdate();
        }
 private void SetLearningModuleListViewItemGroup(LearningModuleListViewItem item)
 {
     if (locationToolStripMenuItem.Checked)
     {
         SetLearningModuleListViewItemGroup(item, ItemOrderType.Location);
     }
     else if (categoryToolStripMenuItem.Checked)
     {
         SetLearningModuleListViewItemGroup(item, ItemOrderType.Category);
     }
     else if (authorToolStripMenuItem.Checked)
     {
         SetLearningModuleListViewItemGroup(item, ItemOrderType.Author);
     }
 }
        /// <summary>
        /// Handles the LearningModuleAdded event of the lmIndex control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="MLifter.BusinessLayer.LearningModuleAddedEventArgs"/> instance containing the event data.</param>
        /// <remarks>Documented by Dev05, 2009-03-07</remarks>
        private void lmIndex_LearningModuleAdded(object sender, LearningModuleAddedEventArgs e)
        {
            if (selectedTreeViewNode == null || (selectedTreeViewNode.Folder == null && !selectedTreeViewNode.IsMainNode) || e.LearningModule == null)
                return;
            LearningModulesIndexEntry entry = e.LearningModule;
            if (selectedTreeViewNode.IsMainNode || entry.Connection == selectedTreeViewNode.Folder.Connection &&
                (!(entry.Connection is UncConnectionStringBuilder) || learningModulesTreeViewControl.ShowLearningModulesOfSubFolder ||
                selectedTreeViewNode.Folder.Path == entry.ConnectionString.ConnectionString.Substring(0, entry.ConnectionString.ConnectionString.LastIndexOf('\\'))))
            {
                lock (actualItems)
                {
                    if (!actualItems.Contains(entry))
                        actualItems.Add(entry);
                }

                listViewLearningModules.Invoke((MethodInvoker)delegate
                {
                    LearningModuleListViewItem item = new LearningModuleListViewItem(entry);
                    listViewLearningModules.BeginUpdate();
                    listViewLearningModules.Items.Add(item);
                    if (listViewLearningModules.SelectedItems.Count == 0)
                        item.Selected = true;
                    item.UpdateDetails();
                    lmEntryListItemList[entry] = item;
                    //entry.IsVerifiedChanged += new EventHandler(LearningModule_IsVerifiedChanged);
                    SetLearningModuleListViewItemGroup(item);
                    listViewLearningModules.EndUpdate();
                });
            }
        }