public void TrackLine(string line)
            {
                ContextEntry item = new ContextEntry(line);

                this.collectedContext.Add(item);
                this.UpdateQueue();
            }
            public void TrackMatch(MatchInfo match)
            {
                ContextEntry item = new ContextEntry(match);

                this.collectedContext.Add(item);
                this.UpdateQueue();
            }
Exemple #3
0
        public void EqualsCheckForUserId()
        {
            // Arrange
            var notifier = new Mock <INotifier>();
            var data     = new TwitterAccountData
            {
                UserId   = 123,
                ImageUrl = "http://example.com/image.png"
            };
            var a = new ContextEntry(notifier.Object, data, null);
            var b = new ContextEntry(notifier.Object, data, null);
            var c = new ContextEntry(notifier.Object, new TwitterAccountData
            {
                UserId   = 111,
                ImageUrl = "http://example.com/image.png"
            }, null);

            // Act
            var ab = a.Equals(b);
            var ba = b.Equals(a);
            var ac = a.Equals(c);

            // ReSharper disable once SuspiciousTypeConversion.Global
            var type = a.Equals(string.Empty);

            // Assert
            Assert.IsTrue(ab);
            Assert.IsTrue(ba);
            Assert.IsFalse(ac);
            Assert.IsFalse(type);
        }
        private static void OnContextMenu(int client, PacketReader reader)
        {
            int type   = reader.ReadInt16();
            int serial = reader.ReadInt32();
            int len    = reader.ReadByte();

            ContextEntry[] ce = new ContextEntry[len];
            int            entry, cliloc, flags, hue;

            switch (type)
            {
            case 1:     // Old Type
                for (int x = 0; x < len; x++)
                {
                    entry  = reader.ReadInt16();
                    cliloc = reader.ReadInt16() + 3000000;
                    flags  = reader.ReadInt16();
                    hue    = 0;

                    if ((flags & 0x20) == 0x20)
                    {
                        hue = reader.ReadInt16();
                    }

                    string text = Cliloc.GetProperty(cliloc);
                    ce[x] = new ContextEntry(client, entry, serial, text, flags, hue);
                }
                IncomingPackets.OnContextMenu(client, ce);
                break;

            case 2:     // KR -> SA3D -> 2D post 7.0.0.0
                for (int x = 0; x < len; x++)
                {
                    cliloc = reader.ReadInt32();
                    entry  = reader.ReadInt16();
                    flags  = reader.ReadInt16();
                    hue    = 0;

                    if ((flags & 0x20) == 0x20)
                    {
                        hue = reader.ReadInt16();
                    }

                    string text = Cliloc.GetProperty(cliloc);
                    ce[x] = new ContextEntry(client, entry, serial, text, flags, hue);
                }
                IncomingPackets.OnContextMenu(client, ce);
                break;
            }
        }
Exemple #5
0
        public void DisposingEntryDisposesContext()
        {
            // Arrange
            var context = new Mock <ITwitterContext>();

            context.Setup(c => c.Dispose()).Verifiable();

            var entry = new ContextEntry(null, null, null, context.Object);

            // Act
            entry.Dispose();

            // Assert
            context.Verify(c => c.Dispose(), Times.Once());
        }
Exemple #6
0
 /// <summary>
 /// try to give resources free
 /// TODO: most likely, this method is not used
 /// </summary>
 /// <param name="ce"></param>
 private static void DisposeData(ContextEntry ce)
 {
     if (ce == null || ce.Screenshot == null)
     {
         return;
     }
     if (ce.Screenshot.Image != null)
     {
         ce.Screenshot.Image.Dispose();
     }
     if (ce.Screenshot != null)
     {
         ce.Screenshot.Dispose();
     }
     ce = null;
 }
        /// <summary>
        /// Gets a screenshot of the active window (already optimised for OCR),
        /// calculates the thresholded image,
        /// runs the OCR,
        /// and returns the result as a context entry.
        /// </summary>
        /// <param name="ce"></param>
        /// <returns></returns>
        public ContextEntry RunOcr(ContextEntry ce)
        {
            if (_tEngine == null)
            {
                return(null);
            }

            try
            {
                // run OCR preprocessing
                RunOcrPreProcessing(ce.Screenshot);

                // processed screenshot
                var processedScreenshot = ce.Screenshot.Image;

                // threshold
                //screenshot = _tEngine.GetThresholdedImage(screenshot, Rectangle.Empty); //TODO: enable or disable?
                //Screenshot.SaveImage(screenshot, "thresholded"); //TEMP

                _tEngine.OcrDone += OcrFinished;
                //var result = _tEngine.DoOCR(screenshot, Rectangle.Empty); // used for single-threading OCR processing
                _tEngine.DoOCR(processedScreenshot, Rectangle.Empty);

                processedScreenshot.Dispose();

                _mEvent = new ManualResetEvent(false);
                _mEvent.WaitOne(); // wait here until it's finished

                // add ocr'd text to context entry
                ce.OcrText    = _temporaryOcrText;
                ce.Confidence = _tempConfidence;

                // reset temp values
                _temporaryOcrText = string.Empty;
                _tempConfidence   = Settings.OcrConfidenceAcceptanceThreshold;

                // release sources
                ce.Screenshot.Dispose();
                ce.Screenshot = null;
            }
            catch (Exception e)
            {
                Logger.WriteToLogFile(e);
            }

            return(ce);
        }
Exemple #8
0
        public void HashCodeIsBaedOnUserId()
        {
            // Arrange
            var notifier = new Mock <INotifier>();
            var data     = new TwitterAccountData
            {
                UserId   = 123,
                ImageUrl = "http://example.com/image.png"
            };
            var entry = new ContextEntry(notifier.Object, data, null);

            // Act
            var hash = entry.GetHashCode();

            // Assert
            Assert.AreEqual(entry.UserId.GetHashCode(), hash);
        }
Exemple #9
0
        /// <summary>
        /// Every x seconds the Context Recognition algorithm is run if the window has changed.
        /// Some info about the context is added (currently: process + windows information)
        /// Then, the background worker is started for OCR recognition
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RunContextRecognition(object sender, EventArgs e)
        {
            // determine whether store or not
            if (_screenshotChangedTracker == null || !_screenshotChangedTracker.WindowChanged())
            {
                return;
            }

            // capture the screenshot
            var sc = ScreenCapture.CaptureActiveWindowHq();

            if (sc == null || sc.Image == null)
            {
                return;                                 // screenshot capture unsuccessful
            }
            // get window & process names (additional contextual information)
            var currentWindowName  = _screenshotChangedTracker.GetWindowText();
            var currentProcessName = _screenshotChangedTracker.GetProcessName();

            var ce = new ContextEntry
            {
                Timestamp   = DateTime.Now,
                WindowName  = currentWindowName,
                ProcessName = currentProcessName,
                Confidence  = OcrLibrary.Settings.OcrConfidenceAcceptanceThreshold, // so it won't be saved if it isn't overwritten
                OcrText     = string.Empty,
                Screenshot  = sc
            };

            // runs the OCR process and get's the result as a context entry
            ce = OcrEngine.GetInstance().RunOcr(ce);

            // only save in database if useful accuracy & enough content
            if (ce != null &&
                ce.Confidence < OcrLibrary.Settings.OcrConfidenceAcceptanceThreshold &&
                ce.OcrText.Length > OcrLibrary.Settings.OcrTextLengthAcceptanceThreshold)
            {
                SaveContextEntryToDatabase(ce);
            }

            // dispose data (to free up RAM)
            DisposeData(ce);
        }
Exemple #10
0
        private static void SaveContextEntryToDatabase(ContextEntry contextEntry)
        {
            if (contextEntry == null ||
                contextEntry.OcrText == string.Empty || // no text recognized
                contextEntry.Confidence < 0.5)          // makes no sense to save if recognition is too in-accurate
            {
                return;
            }

            var query = "INSERT INTO " + Settings.DbTable +
                        " (time, timestamp, ocrText, confidentality, windowName, processName) VALUES (strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime'), " +
                        Database.GetInstance().QTime(contextEntry.Timestamp) + ", " +
                        Database.GetInstance().Q(contextEntry.OcrText) + ", " +
                        Database.GetInstance().Q(contextEntry.Confidence.ToString()) + ", " +
                        Database.GetInstance().Q(contextEntry.WindowName) + ", " +
                        Database.GetInstance().Q(contextEntry.ProcessName) + ");";

            Database.GetInstance().ExecuteDefaultQuery(query);
        }
        private static void OnContextMenu(int client, PacketReader reader)
        {
            int type = reader.ReadInt16();
            int serial = reader.ReadInt32();
            int len = reader.ReadByte();

            ContextEntry[] ce = new ContextEntry[len];
            int entry, cliloc, flags, hue;

            switch (type)
            {
                case 1: // Old Type
                    for (int x = 0; x < len; x++)
                    {
                        entry = reader.ReadInt16();
                        cliloc = reader.ReadInt16() + 3000000;
                        flags = reader.ReadInt16();
                        hue = 0;

                        if ((flags & 0x20) == 0x20)
                            hue = reader.ReadInt16();

                        string text = Cliloc.GetProperty(cliloc);
                        ce[x] = new ContextEntry(client, entry, serial, text, flags, hue);
                    }
                    IncomingPackets.OnContextMenu(client, ce);
                    break;
                case 2: // KR -> SA3D -> 2D post 7.0.0.0
                    for (int x = 0; x < len; x++)
                    {
                        cliloc = reader.ReadInt32();
                        entry = reader.ReadInt16();
                        flags = reader.ReadInt16();
                        hue = 0;

                        if ((flags & 0x20) == 0x20)
                            hue = reader.ReadInt16();

                        string text = Cliloc.GetProperty(cliloc);
                        ce[x] = new ContextEntry(client, entry, serial, text, flags, hue);
                    }
                    IncomingPackets.OnContextMenu(client, ce);
                    break;
            }
        }
 internal static void OnContextMenu(int client, ContextEntry[] contextEntries)
 {
     dContextMenu handler = InternalContextMenuEvent;
     if (handler != null) handler(client, contextEntries);
     lock (myContextMenuLock)
     {
         handler = myContextMenuEvent;
         try { if (handler != null) handler(client, contextEntries); }
         catch (Exception ex) { Log.LogMessage(ex); }
     }
 }
Exemple #13
0
        public void InitializeUI()
        {
            #region Volume

            var volumeUp = new ContextEntry()
            {
                Name            = "Up",
                Icon            = "speaker-volume-control-up",
                ClickedCallback = VolumeUpCallBack
            };

            var volumeDown = new ContextEntry()
            {
                Name            = "Down",
                Icon            = "speaker-volume-control",
                ClickedCallback = VolumeDownCallBack
            };

            var volumeMute = new ContextEntry()
            {
                Name            = "Mute",
                Icon            = "speaker-volume-control-mute",
                ClickedCallback = VolumeMuteCallBack
            };

            var volume = new ContextEntry()
            {
                Name     = "Volume",
                Icon     = "speaker-volume",
                Children = new[] { volumeUp, volumeDown, volumeMute }
            };


            #endregion
            #region Monitor

            var rotate90CW = new ContextEntry()
            {
                Name            = "Rotate 90 CW",
                Icon            = "arrow-turn",
                ClickedCallback = rotate90CWCallBack
            };

            var turnMonitorOff = new ContextEntry()
            {
                Name            = "Turn Off",
                Icon            = "remove",
                ClickedCallback = turnMonitorOffCallBack
            };

            var turnMonitorOn = new ContextEntry()
            {
                Name            = "Turn On",
                Icon            = "tick",
                ClickedCallback = turnMonitorOnCallback
            };

            var monitor = new ContextEntry()
            {
                Name            = "Monitor",
                Icon            = "television",
                ClickedCallback = null,
                Children        = new[] { turnMonitorOff, turnMonitorOn, rotate90CW }
            };

            #endregion

            var aio = new ContextEntry()
            {
                Name            = "AIO",
                Icon            = "arrow-in",
                ClickedCallback = null,
                Children        = new[] { monitor, volume }
            };

            UIHost.AddContextEntry(aio);
        }