Smuxi.Frontend.Gnome.ChatViewManager.GetSortedChatPosition C# (CSharp) Method

GetSortedChatPosition() private method

private GetSortedChatPosition ( ChatView chatView ) : int
chatView ChatView
return int
        int GetSortedChatPosition(ChatView chatView)
        {
            // starting with > 0.8 the Engine supplies ChatModel.Position for us
            if (Frontend.EngineProtocolVersion > new Version("0.8")) {
                return chatView.Position;
            }

            // COMPAT: Engine <= 0.8 doesn't populate ChatModel.Position thus
            // _we_ have to find a good position
            var chat = chatView.ChatModel;
            // REMOTING CALL 1
            int idx = chat.Position;
            // REMOTING CALL 2
            ChatType type = chat.ChatType;
            // new group person and group chats behind their protocol chat
            if (idx == -1 &&
                (type == ChatType.Person ||
                 type == ChatType.Group)) {
                // REMOTING CALL 3
                IProtocolManager pm = chat.ProtocolManager;
                for (int i = 0; i < f_Notebook.NPages; i++) {
                    ChatView page = (ChatView) f_Notebook.GetNthPage(i);
                    ChatModel pageChat = page.ChatModel;
                    // REMOTING CALL 4 and 5
                    if (pageChat.ChatType == ChatType.Protocol &&
                        pageChat.ProtocolManager == pm) {
                        idx = i + 1;
                        break;
                    }
                }

                if (idx != -1) {
                    // now find the first chat with a different protocol manager
                    bool found = false;
                    for (int i = idx; i < f_Notebook.NPages; i++) {
                        ChatView page = (ChatView) f_Notebook.GetNthPage(i);
                        ChatModel pageChat = page.ChatModel;
                        // REMOTING CALL 6
                        if (pageChat.ProtocolManager != pm) {
                            found = true;
                            idx = i;
                            break;
                        }
                    }
                    if (!found) {
                        // if there was no next protocol manager, simply append
                        // the chat way to the end
                        idx = -1;
                    }
                }
            }

            return idx;
        }