~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to src/Frontend/NickCompleter.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Smuxi - Smart MUltipleXed Irc
 
3
 *
 
4
 * Copyright (c) 2005-2013 Mirco Bauer <meebey@meebey.net>
 
5
 * Copyright (c) 2013 Ondra Hosek <ondra.hosek@gmail.com>
 
6
 *
 
7
 * Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 
22
 */
 
23
 
 
24
using System;
 
25
using System.Collections.Generic;
 
26
using Smuxi.Engine;
 
27
 
 
28
namespace Smuxi.Frontend
 
29
{
 
30
    /// <summary>
 
31
    /// Automatically completes nicknames (e.g. when the user presses the Tab key).
 
32
    /// </summary>
 
33
    public abstract class NickCompleter
 
34
    {
 
35
        public string CompletionChar = ":";
 
36
 
 
37
        /// <summary>
 
38
        /// Isolates the nickname that should be completed.
 
39
        /// </summary>
 
40
        /// <returns>The isolated nickname.</returns>
 
41
        /// <param name="entryLine">The text currently typed into the input text box.</param>
 
42
        /// <param name="cursorPosition">The current location of the cursor in the input text box.</param>
 
43
        /// <param name="nickBeginning">Stores where the isolated nickname begins in the entered text.</param>
 
44
        /// <param name="appendSpace">Whether to append a space when the nickname is completed.</param>
 
45
        /// <param name="leadingAt">Whether the nickname started with a leading @ (which was stripped away).</param>
 
46
        protected static string IsolateNickToComplete(string entryLine, int cursorPosition, out int nickBeginning, out bool appendSpace, out bool leadingAt)
 
47
        {
 
48
            string ret;
 
49
            int prev_space = entryLine.Substring(0, cursorPosition).LastIndexOf(' ');
 
50
            int next_space = entryLine.IndexOf(' ', cursorPosition);
 
51
            appendSpace = false;
 
52
 
 
53
            if (prev_space == -1 && next_space == -1) {
 
54
                // no spaces (the nick is the only thing)
 
55
                nickBeginning = 0;
 
56
                appendSpace = true;
 
57
                ret = entryLine;
 
58
            } else if (prev_space == -1) {
 
59
                nickBeginning = 0;
 
60
                ret = entryLine.Substring(0, next_space);
 
61
            } else if (next_space == -1) {
 
62
                nickBeginning = prev_space + 1;
 
63
                appendSpace = true;
 
64
                ret = entryLine.Substring(nickBeginning);
 
65
            } else {
 
66
                nickBeginning = prev_space + 1;
 
67
                ret = entryLine.Substring(prev_space + 1, next_space - prev_space - 1);
 
68
            }
 
69
 
 
70
            leadingAt = false;
 
71
            if (ret.StartsWith("@")) {
 
72
                leadingAt = true;
 
73
                ++nickBeginning;
 
74
                ret = ret.Substring(1);
 
75
            }
 
76
 
 
77
            return ret;
 
78
        }
 
79
 
 
80
        /// <summary>
 
81
        /// Returns a list containing only the nicknames matching the given prefix.
 
82
        /// </summary>
 
83
        /// <returns>
 
84
        /// The (sorted) list of nicknames matching the given prefix.
 
85
        /// </returns>
 
86
        /// <param name="persons">List of people to enumerate.</param>
 
87
        /// <param name="prefix">Prefix of nicknames to return.</param>
 
88
        protected static IList<string> NicksMatchingPrefix(IList<PersonModel> persons, string prefix)
 
89
        {
 
90
            var ret = new List<string>();
 
91
            string lowerPfx = prefix.ToLower();
 
92
            foreach (PersonModel person in persons) {
 
93
                string nick = person.IdentityName;
 
94
                if (nick.ToLower().StartsWith(lowerPfx)) {
 
95
                    ret.Add(nick);
 
96
                }
 
97
            }
 
98
            ret.Sort();
 
99
            return ret;
 
100
        }
 
101
 
 
102
        /// <summary>
 
103
        /// Performs nickname tab completion on the specified input.
 
104
        /// </summary>
 
105
        /// <param name="entryLine">The text currently typed into the input text box.</param>
 
106
        /// <param name="cursorPosition">
 
107
        /// The current location of the cursor in the input text box. Equal to the index of the
 
108
        /// character after the current cursor position.
 
109
        /// </param>
 
110
        /// <param name="currentChatView">
 
111
        /// The current chat view. The list of participants is fetched from it; the completer may
 
112
        /// also append messages to the chat to provide further information.
 
113
        /// </param>
 
114
        abstract public void Complete(ref string entryLine, ref int cursorPosition, IChatView currentChatView);
 
115
    }
 
116
}