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

« back to all changes in this revision

Viewing changes to src/Frontend-GNOME/Entry.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
1
/*
2
2
 * Smuxi - Smart MUltipleXed Irc
3
3
 *
4
 
 * Copyright (c) 2005-2012 Mirco Bauer <meebey@meebey.net>
 
4
 * Copyright (c) 2005-2013 Mirco Bauer <meebey@meebey.net>
5
5
 *
6
6
 * Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
7
7
 *
43
43
        private CommandManager   _CommandManager;
44
44
        private new EntrySettings Settings { get; set; }
45
45
 
 
46
        private NickCompleter NickCompleter { get; set; }
 
47
 
46
48
        ChatViewManager ChatViewManager;
47
49
        event EventHandler<EventArgs> Activated;
48
50
 
243
245
        
244
246
        protected virtual void ProcessKey(Gtk.KeyPressEventArgs e)
245
247
        {
246
 
            if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
247
 
                Text.Length == 0) {
 
248
            if (Frontend.IsWindows && String.IsNullOrEmpty(Text)) {
248
249
                // HACK: workaround rendering issue on Windows where the text
249
250
                // cursor and first typed character are not showing up until
250
251
                // a 2nd character is typed, see #810
282
283
                            ChatViewManager.CurrentChatNumber++;
283
284
                        }
284
285
                        break;
 
286
                    case Gdk.Key.c:
 
287
                    case Gdk.Key.C:
 
288
                        // only use copy if something is selected in the entry
 
289
                        if (Buffer.HasSelection) {
 
290
                            e.RetVal = false;
 
291
                            break;
 
292
                        }
 
293
                        // copy selection from main chat window
 
294
                        var buf = ChatViewManager.CurrentChatView.OutputMessageTextView.Buffer;
 
295
                        buf.CopyClipboard(Gtk.Clipboard.Get(Gdk.Selection.Clipboard));
 
296
                        break;
285
297
                    // don't break unicode input
286
298
                    case Gdk.Key.U:
287
 
                    // don't break copy/paste
288
 
                    case Gdk.Key.c:
289
 
                    case Gdk.Key.C:
 
299
                    // don't break paste
290
300
                    case Gdk.Key.v:
291
301
                    case Gdk.Key.V:
292
302
                    // don't break select all
305
315
                    case Gdk.Key.End:
306
316
                        ChatViewManager.CurrentChatView.ScrollToEnd();
307
317
                        break;
 
318
                    // anything else we let GTK+ handle
 
319
                    default:
 
320
                        e.RetVal = false;
 
321
                        break;
308
322
                }
309
323
            }
310
324
            
357
371
                        pagenumber = 19;
358
372
                        break;
359
373
                }
 
374
                switch (key) {
 
375
                    case Gdk.Key.h:
 
376
                    case Gdk.Key.H:
 
377
                        if (Frontend.IsMacOSX) {
 
378
                            Frontend.MainWindow.Iconify();
 
379
                            e.RetVal = true;
 
380
                        }
 
381
                        break;
 
382
                    case Gdk.Key.braceleft:
 
383
                    case Gdk.Key.Up:
 
384
                        if (Frontend.IsMacOSX) {
 
385
                            ChatViewManager.CurrentChatNumber--;
 
386
                            e.RetVal = true;
 
387
                        }
 
388
                        break;
 
389
                    case Gdk.Key.braceright:
 
390
                    case Gdk.Key.Down:
 
391
                        if (Frontend.IsMacOSX) {
 
392
                            ChatViewManager.CurrentChatNumber++;
 
393
                            e.RetVal = true;
 
394
                        }
 
395
                        break;
 
396
                }
 
397
 
360
398
 
361
399
                if (pagenumber != -1) {
362
400
                    ChatViewManager.CurrentChatNumber = pagenumber;
432
470
                }
433
471
                
434
472
                if (Text.IndexOf("\n") != -1) {
 
473
                    var text = Text.TrimEnd('\n');
435
474
                    // seems to be a paste, so let's break it apart
436
 
                    string[] msgParts = Text.Split(new char[] {'\n'});
 
475
                    string[] msgParts = text.Split(new char[] {'\n'});
437
476
                    if (msgParts.Length > 3) {
438
477
                        string msg = String.Format(_("You are going to paste {0} lines. Do you want to continue?"),
439
478
                                                   msgParts.Length);
450
489
                            return;
451
490
                        }
452
491
                    }
453
 
                    foreach (string msg in msgParts) {
454
 
                        ExecuteCommand(msg);
 
492
                    if (Frontend.EngineVersion < new Version(0,8,11)) {
 
493
                        foreach (string msg in msgParts) {
 
494
                            ExecuteCommand(msg);
 
495
                        }
 
496
                    } else {
 
497
                        // new engines know how to handle messages containing \n
 
498
                        ExecuteCommand(text);
455
499
                    }
456
500
                } else {
457
501
                    ExecuteCommand(Text);
511
555
                        _CommandEcho(cd);
512
556
                        handled = true;
513
557
                        break;
514
 
                    case "exec":
515
 
                        _CommandExec(cd);
516
 
                        handled = true;
517
 
                        break;
518
558
                    case "window":
519
559
                        _CommandWindow(cd);
520
560
                        handled = true;
531
571
                        _CommandSync(cd);
532
572
                        handled = true;
533
573
                        break;
 
574
                    case "gc":
 
575
                        GC.Collect();
 
576
                        handled = true;
 
577
                        break;
 
578
                    case "generate_messages":
 
579
                        CommandGenerateMessages(cd);
 
580
                        handled = true;
 
581
                        break;
534
582
                }
535
583
            }
536
584
            
567
615
 
568
616
        private void _CommandList(CommandModel cd)
569
617
        {
570
 
            Frontend.MainWindow.OpenFindGroupChatWindow(cd.Parameter);
 
618
            Frontend.OpenFindGroupChatWindow(cd.Parameter);
571
619
        }
572
620
 
573
621
        private void _CommandDetach(CommandModel cd)
577
625
 
578
626
        private void _CommandEcho(CommandModel cd)
579
627
        {
580
 
            cd.FrontendManager.AddTextToChat(
581
 
                cd.Chat,
582
 
                String.Format("-!- {0}", cd.Parameter)
583
 
            );
584
 
        }
585
 
        
586
 
        private void _CommandExec(CommandModel cd)
587
 
        {
588
 
            if (cd.DataArray.Length >= 2) {
589
 
                string output;
590
 
                System.Diagnostics.Process process = new System.Diagnostics.Process();
591
 
                process.StartInfo.FileName = cd.DataArray[1];
592
 
                if (cd.DataArray.Length >= 3) { 
593
 
                    process.StartInfo.Arguments = String.Join(" ", cd.DataArray,
594
 
                                                    2, cd.DataArray.Length-2);
595
 
                }
596
 
                process.StartInfo.RedirectStandardOutput = true;
597
 
                process.StartInfo.RedirectStandardError = true;
598
 
                process.StartInfo.UseShellExecute = false;
599
 
                
600
 
                try {
601
 
                    process.Start();
602
 
                    output = process.StandardOutput.ReadToEnd();
603
 
                    cd.FrontendManager.AddTextToChat(cd.Chat, output);
604
 
                } catch {
605
 
                }
606
 
            }
 
628
            var msg = new MessageBuilder().
 
629
                AppendEventPrefix().
 
630
                AppendText(cd.Parameter).
 
631
                ToMessage();
 
632
            cd.FrontendManager.AddMessageToChat(cd.Chat, msg);
607
633
        }
608
634
    
609
635
        private void _CommandWindow(CommandModel cd)
610
636
        {
611
 
            FrontendManager fm = cd.FrontendManager;
612
637
            if (cd.DataArray.Length >= 2) {
613
638
                var currentChat = ChatViewManager.CurrentChatView;
614
639
                if (cd.Parameter.ToLower() == "close") {
684
709
            ChatViewManager.CurrentChatView.Clear();
685
710
        }
686
711
 
 
712
        void CommandGenerateMessages(CommandModel cd)
 
713
        {
 
714
            var count = 0;
 
715
            Int32.TryParse(cd.Parameter, out count);
 
716
 
 
717
            var chat = ChatViewManager.CurrentChatView;
 
718
            var builder = new MessageBuilder();
 
719
            var sender = new ContactModel("msg-tester", "msg-tester", "test", "test");
 
720
            builder.AppendMessage(sender, "time for a messsage generator command so I can test speed and memory usage");
 
721
            var text = builder.CreateText(" *formatted text* ");
 
722
            text.Bold = true;
 
723
            builder.Append(text);
 
724
            builder.AppendUrl("https://www.smuxi.org/");
 
725
 
 
726
            var msgs = new List<MessageModel>(count);
 
727
            for (var i = 0; i < count; i++) {
 
728
                var msg = builder.ToMessage();
 
729
                msgs.Add(msg);
 
730
            }
 
731
 
 
732
            DateTime start, stop;
 
733
            start = DateTime.UtcNow;
 
734
            foreach (var msg in msgs) {
 
735
                chat.AddMessage(msg);
 
736
            }
 
737
            stop = DateTime.UtcNow;
 
738
 
 
739
            builder = new MessageBuilder();
 
740
            builder.AppendText(
 
741
                "ChatView.AddMessage(): count: {0} took: {1:0} ms avg: {2:0.00} ms",
 
742
                count,
 
743
                (stop - start).TotalMilliseconds,
 
744
                (stop - start).TotalMilliseconds / count
 
745
            );
 
746
            chat.AddMessage(builder.ToMessage());
 
747
 
 
748
            builder = new MessageBuilder();
 
749
            builder.AppendText(
 
750
                "ChatView.AddMessage(): MessageTextTagTable.Size: {0}",
 
751
                chat.OutputMessageTextView.MessageTextTagTable.Size
 
752
            );
 
753
            chat.AddMessage(builder.ToMessage());
 
754
        }
 
755
 
687
756
        private void _NickCompletion()
688
757
        {
689
 
            // return if we don't support the current ChatView
690
 
            if (!(ChatViewManager.CurrentChatView is GroupChatView) &&
691
 
                !(ChatViewManager.CurrentChatView is PersonChatView)) {
692
 
                return;
693
 
            }
694
 
 
 
758
            // perform completion
 
759
            string text = Text;
695
760
            int position = Position;
696
 
            string text = Text;
697
 
            string word;
698
 
            int previous_space;
699
 
            int next_space;
700
 
 
701
 
            // find the current word
702
 
            string temp;
703
 
            temp = text.Substring(0, position);
704
 
            previous_space = temp.LastIndexOf(' ');
705
 
            next_space = text.IndexOf(' ', position);
706
 
 
707
 
#if LOG4NET
708
 
            _Logger.Debug("previous_space: "+previous_space);
709
 
            _Logger.Debug("next_space: "+next_space);
710
 
#endif
711
 
 
712
 
            if (previous_space != -1 && next_space != -1) {
713
 
                // previous and next space exist
714
 
                word = text.Substring(previous_space + 1, next_space - previous_space - 1);
715
 
            } else if (previous_space != -1) {
716
 
                // previous space exist
717
 
                word = text.Substring(previous_space + 1);
718
 
            } else if (next_space != -1) {
719
 
                // next space exist
720
 
                word = text.Substring(0, next_space);
721
 
            } else {
722
 
                // no spaces
723
 
                word = text;
724
 
            }
725
 
 
726
 
            if (word == String.Empty) {
727
 
                return;
728
 
            }
729
 
 
730
 
            bool leadingAt = false;
731
 
            // remove leading @ character
732
 
            if (word.StartsWith("@")) {
733
 
                word = word.Substring(1);
734
 
                leadingAt = true;
735
 
            }
736
 
 
737
 
            // find the possible nickname
738
 
            bool found = false;
739
 
            bool partial_found = false;
740
 
            string nick = null;
741
 
 
742
 
            ChatView view = ChatViewManager.CurrentChatView;
743
 
            if (view is GroupChatView) {
744
 
                GroupChatView cp = (GroupChatView) view;
745
 
                if (Settings.BashStyleCompletion) {
746
 
                    IList<string> result = cp.PersonLookupAll(word);
747
 
                    if (result == null || result.Count == 0) {
748
 
                        // no match
749
 
                    } else if (result.Count == 1) {
750
 
                        found = true;
751
 
                        nick = result[0];
752
 
                    } else if (result.Count >= 2) {
753
 
                        string[] nickArray = new string[result.Count];
754
 
                        result.CopyTo(nickArray, 0);
755
 
                        string nicks = String.Join(" ", nickArray, 1, nickArray.Length - 1);
756
 
                        ChatViewManager.CurrentChatView.AddMessage(
757
 
                            new MessageModel(String.Format("-!- {0}", nicks))
758
 
                        );
759
 
                        found = true;
760
 
                        partial_found = true;
761
 
                        nick = result[0];
762
 
                    }
763
 
                } else {
764
 
                    PersonModel person = cp.PersonLookup(word);
765
 
                    if (person != null) {
766
 
                        found = true;
767
 
                        nick = person.IdentityName;
768
 
                     }
769
 
                }
770
 
            } else {
771
 
                var personChat = (PersonChatView) ChatViewManager.CurrentChatView;
772
 
                var personModel = personChat.PersonModel;
773
 
                if (personModel.IdentityName.StartsWith(word,
774
 
                        StringComparison.InvariantCultureIgnoreCase)) {
775
 
                    found = true;
776
 
                    nick = personModel.IdentityName;
777
 
                }
778
 
            }
779
 
 
780
 
            string completionChar = Settings.CompletionCharacter;
781
 
            // add leading @ back and supress completion character
782
 
            if (leadingAt) {
783
 
                word = String.Format("@{0}", word);
784
 
                nick = String.Format("@{0}", nick);
785
 
                completionChar = String.Empty;
786
 
            }
787
 
 
788
 
            if (found) {
789
 
                // put the found nickname in place
790
 
                if (previous_space != -1 && next_space != -1) {
791
 
                    // previous and next space exist
792
 
                    temp = text.Remove(previous_space + 1, word.Length);
793
 
                    temp = temp.Insert(previous_space + 1, nick);
794
 
                    Text = temp;
795
 
                    if (partial_found) {
796
 
                        Position = previous_space + 1 + nick.Length;
797
 
                    } else {
798
 
                        Position = previous_space + 2 + nick.Length;
799
 
                    }
800
 
                } else if (previous_space != -1) {
801
 
                    // only previous space exist
802
 
                    temp = text.Remove(previous_space + 1, word.Length);
803
 
                    temp = temp.Insert(previous_space + 1, nick);
804
 
                    if (partial_found) {
805
 
                        Text = temp;
806
 
                    } else {
807
 
                        Text = temp + " ";
808
 
                    }
809
 
                    Position = previous_space + 2 + nick.Length;
810
 
                } else if (next_space != -1) {
811
 
                    // only next space exist
812
 
                    temp = text.Remove(0, next_space + 1);
813
 
                    if (partial_found) {
814
 
                        Text = nick + " " + temp;
815
 
                        Position = nick.Length;
816
 
                    } else {
817
 
                        Text = String.Format("{0}{1} {2}", nick,
818
 
                                             completionChar, temp);
819
 
                        Position = nick.Length + completionChar.Length + 1;
820
 
                    }
821
 
                } else {
822
 
                    // no spaces
823
 
                    if (partial_found) {
824
 
                        Text = nick;
825
 
                    } else {
826
 
                        Text = String.Format("{0}{1} ", nick, completionChar);
827
 
                    }
828
 
                    Position = -1;
829
 
                }
830
 
            }
 
761
            NickCompleter.Complete(ref text, ref position, ChatViewManager.CurrentChatView);
 
762
            Text = text;
 
763
            Position = position;
831
764
        }
832
765
        
833
766
        public virtual void ApplyConfig(UserConfig config)
852
785
            ModifyFont(theme.FontDescription);
853
786
 
854
787
            Settings.ApplyConfig(config);
 
788
 
 
789
            // replace nick completer if needed
 
790
            if (Settings.BashStyleCompletion && !(NickCompleter is LongestPrefixNickCompleter)) {
 
791
                NickCompleter = new LongestPrefixNickCompleter();
 
792
            } else if (!Settings.BashStyleCompletion && !(NickCompleter is TabCycleNickCompleter)) {
 
793
                NickCompleter = new TabCycleNickCompleter();
 
794
            }
 
795
 
 
796
            // set the completion character
 
797
            NickCompleter.CompletionChar = Settings.CompletionCharacter;
855
798
        }
856
799
 
857
800
        private void InitCommandManager()