private void Apply() { if (_forumListView.FocusedItem != null) { _forumListView.FocusedItem.SubItems[(int)Column.Priority].Text = _priority.Value.ToString(); } var subscriptionRequests = new List <ForumSubscriptionRequest>(); foreach (ListViewItem item in _forumListView.Items) { var forum = _forumList[(int)item.Tag]; if (forum.Subscribed != item.Checked) { subscriptionRequests.Add(new ForumSubscriptionRequest(forum.ID, item.Checked)); } var newPriority = int.Parse(item.SubItems[0].Text); if (item.Checked && forum.Priority != newPriority) { ForumsSubscriptionHelper.UpdateForumPriority(_provider, forum.ID, newPriority); } } if (subscriptionRequests.Count > 0) { ForumsSubscriptionHelper.UpdateForumsSubscriptions(_provider, subscriptionRequests, true); } //без рефреша возникало исключение если нажать "применить" потом "ок" RefreshForm(); }
public void ExecuteSubscribeOrUnsubscribeForum( ICommandContext context, int?forumId) { forumId = GetForumId(context, forumId); ForumsSubscriptionHelper.UpdateForumsSubscriptions( context, new[] { new ForumSubscriptionRequest( forumId.Value, !context.GetRequiredService <IRsdnForumService>().IsForumSubscribed(forumId.Value)) }, true); }
/// <summary> /// Добавление сообшений полученыых с сервера в базу /// </summary> /// <param name="provider"></param> /// <param name="db"></param> /// <param name="messages">Добавляемые сообщения</param> /// <param name="selfid"></param> /// <param name="progressHandler">обработчик прогресса обработки сообщений</param> /// <param name="updatedTopicIds">Какие топики были обновлены.</param> /// <param name="updatedMessageIds">Какие сообщения были добавлены.</param> private static void AddMessages( [NotNull] IServiceProvider provider, IDataContext db, JanusMessageInfo[] messages, int selfid, Action <int, int> progressHandler, out int[] updatedTopicIds, out int[] updatedMessageIds) { if (db == null) { throw new ArgumentNullException("db"); } if (messages == null) { throw new ArgumentNullException("messages"); } var msgIds = new List <int>(); var topicIds = new List <int>(); var processed = 0; foreach (var msg in messages) { if (ForumsSubscriptionHelper.IsTrashForum(msg.forumId)) { try { var mid = msg.messageId; db.TopicInfos(ti => ti.MessageID == mid).Delete(); db.Messages(m => m.ID == mid).Delete(); } catch (Exception) { provider.LogError(Resources.ErrorOnMessageDelete + msg.messageId); } } else { var updatedTid = db.Message(msg.messageId, m => (int?)m.TopicID); if (msg.message.IsNullOrEmpty()) { msg.message = "<none>"; } var lastModerated = msg.lastModerated == DateTime.MinValue ? null : (DateTime?)msg.lastModerated; try { if (!updatedTid.HasValue) { var markRead = false; if (msg.parentId != 0) { markRead = db.Message(msg.parentId, m => m.ReadReplies); } var isRead = msg.userId == selfid ? provider.GetRequiredService <IRsdnSyncConfigService>().GetConfig().MarkOwnMessages : markRead; db .Messages() .Value(_ => _.ID, msg.messageId) .Value(_ => _.ForumID, msg.forumId) .Value(_ => _.TopicID, msg.topicId) .Value(_ => _.ParentID, msg.parentId) .Value(_ => _.Date, msg.messageDate) .Value(_ => _.UserNick, msg.userNick) .Value(_ => _.Subject, msg.subject) .Value(_ => _.Message, msg.message) .Value(_ => _.UserClass, ToJanusUserClass(msg.userRole)) .Value(_ => _.IsRead, isRead) .Value(_ => _.UserID, msg.userId) .Value(_ => _.ArticleId, msg.articleId) .Value(_ => _.ReadReplies, markRead) .Value(_ => _.Name, msg.messageName) .Value(_ => _.LastModerated, lastModerated) .Value(_ => _.Closed, msg.closed) .Insert(); msgIds.Add(msg.messageId); } else { var locMsg = msg; db .Messages(m => m.ID == locMsg.messageId) .Set(_ => _.ForumID, msg.forumId) .Set(_ => _.TopicID, msg.topicId) .Set(_ => _.ParentID, msg.parentId) .Set(_ => _.Date, msg.messageDate) .Set(_ => _.UserID, msg.userId) .Set(_ => _.UserNick, msg.userNick) .Set(_ => _.Subject, msg.subject) .Set(_ => _.Message, msg.message) .Set(_ => _.UserClass, ToJanusUserClass(msg.userRole)) .Set(_ => _.ArticleId, msg.articleId) .Set(_ => _.LastModerated, lastModerated) .Set(_ => _.Name, msg.messageName) .Set(_ => _.Closed, msg.closed) .Update(); topicIds.Add(updatedTid.Value == 0 ? msg.messageId : updatedTid.Value); } } catch (Exception e) { // Какая ....! provider.LogError( string.Format( "{0}{1} : {2}", Resources.ErrorOnMessageProcessing, msg.messageId, e.Message)); } } processed++; if (progressHandler != null) { progressHandler(messages.Length, processed); } } db .SubscribedForums() .Set(_ => _.LastSync, 1) .Update(); updatedTopicIds = topicIds.ToArray(); updatedMessageIds = msgIds.ToArray(); }
private static void AddBrokenTopicRequests( IServiceProvider provider, IEnumerable <JanusMessageInfo> messages) { var freshMids = new Dictionary <int, JanusMessageInfo>(); var parentIds = new Dictionary <int, int>(); // Собираем все messageId и parentId foreach (var msg in messages) { freshMids[msg.messageId] = msg; if (msg.parentId != 0 && !ForumsSubscriptionHelper.IsTrashForum(msg.forumId)) // Skip roots & trash { parentIds[msg.parentId] = msg.messageId; } } // Удаляем тех родителей, которые уже присутствуют в том же пакете foreach (var parentId in parentIds.Keys.ToArray()) { if (freshMids.ContainsKey(parentId)) { parentIds.Remove(parentId); } } using (var dbMgr = provider.CreateDBContext()) { var ids = dbMgr .Messages() .Where(msg => parentIds.Keys.ToArray().Contains(msg.ID)) .Select(msg => msg.ID); // Удаляем тех родителей, которые присутствуют в БД foreach (var mid in ids) { parentIds.Remove(mid); } } // Оборванных веток нет - выходим if (parentIds.Count <= 0) { return; } provider.LogInfo( string.Format( parentIds.Count.GetDeclension( Resources.BrokenTopicMessage1, Resources.BrokenTopicMessage2, Resources.BrokenTopicMessage5), parentIds.Count, parentIds.Values.JoinToStringSeries(30).FirstOrDefault())); // Добавляем оставшиеся в запросы топиков // TODO: вменяемые строковые константы foreach (var mid in parentIds.Values) { provider .GetOutboxManager() .DownloadTopics .Add( Resources.BrokenTopicRequestSource, mid, Resources.BrokenTopicRequestHint.FormatStr(freshMids[mid].subject)); } }
private void InitListView(bool preserveCheckStates) { var currentCheckStates = new Dictionary <string, bool>(); if (preserveCheckStates) { foreach (ListViewItem lvItem in _forumListView.Items) { currentCheckStates[lvItem.SubItems[1].Text] = lvItem.Checked; } } using (var db = _provider.CreateDBContext()) { db .SubscribedForums(f => f.Priority == null) .Set(_ => _.Priority, 0) .Update(); var forums = ForumsSubscriptionHelper.GetAllForums( db, f => new { f.ID, f.Name, f.Descript, Priority = 0, Subscribed = false }, f => new { f.ID, f.Name, f.Descript, Priority = f.Priority ?? 0, Subscribed = true }); switch (_sortType) { case SortType.ByDesc: forums = forums.OrderBy(f => f.Descript); break; case SortType.ByName: forums = forums.OrderBy(f => f.Name); break; case SortType.ByPriorityAndName: forums = forums.OrderBy(f => f.Priority).ThenBy(f => f.Name); break; case SortType.ByPriorityAndDesc: forums = forums.OrderBy(f => f.Priority).ThenBy(f => f.Descript); break; default: throw new ArgumentOutOfRangeException(); } _forumList = forums .Select(f => new ForumData(f.ID, f.Name, f.Descript, f.Priority, f.Subscribed)) .ToList(); } using (_forumListView.UpdateScope(true)) for (var i = 0; i < _forumList.Count; i++) { var forum = _forumList[i]; var lvi = new ListViewItem( new[] { forum.Priority.ToString(), forum.Name, forum.Descript }) { Checked = forum.Subscribed, ImageIndex = 0, Tag = i }; // restore user changes if (currentCheckStates.ContainsKey(forum.Name)) { lvi.Checked = currentCheckStates[forum.Name]; } _forumListView.Items.Add(lvi); } if (_forumList.Count > 0) { _priority.Value = _forumList[0].Priority; } }