Beispiel #1
0
    def got_screen_change(self, process_name, window_name, win_x, win_y, win_width, win_height):
        """Receives a screen change and stores any changes.
        If the process or window has changed it will also store any queued pressed keys.
        Keyword arguments:
        process_name -- the name of the process running the current window
        window_name -- the name of the window
        win_x -- the x position of the window
        win_y -- the y position of the window
        win_width -- the width of the window
        win_height -- the height of the window"""

        # skip the event if same arguments as last time are passed
        args = [process_name, window_name, win_x, win_y, win_width, win_height]
        if self.last_screen_change == args:
            return

        self.last_screen_change = args

        cur_process = self.session.query(
            Process
        ).filter_by(
            name=process_name
        ).scalar()
        if not cur_process:
            cur_process = Process(process_name)
            self.session.add(cur_process)

        cur_geometry = self.session.query(
            Geometry
        ).filter_by(
            xpos=win_x,
            ypos=win_y,
            width=win_width,
            height=win_height
        ).scalar()
        if not cur_geometry:
            cur_geometry = Geometry(win_x, win_y, win_width, win_height)
            self.session.add(cur_geometry)

        cur_window = self.session.query(Window).filter_by(title=window_name,
                                                          process_id=cur_process.id).scalar()
        if not cur_window:
            cur_window = Window(window_name, cur_process.id)
            self.session.add(cur_window)

        if not (self.current_window.proc_id == cur_process.id
                and self.current_window.win_id == cur_window.id):
            self.trycommit()
            self.store_keys()  # happens before as these keypresses belong to the previous window
            self.current_window.proc_id = cur_process.id
            self.current_window.win_id = cur_window.id
            self.current_window.geo_id = cur_geometry.id
Beispiel #2
0
    def got_screen_change(self, process_name, window_name, win_x, win_y,
                          win_width, win_height, browser_url, regularApps,
                          regularWindows):
        """ Receives a screen change and stores any changes. If the process or window has
            changed it will also store any queued pressed keys.
            process_name is the name of the process running the current window
            window_name is the name of the window
            win_x is the x position of the window
            win_y is the y position of the window
            win_width is the width of the window
            win_height is the height of the window """

        # find apps that have opened or become active since the last check
        for app in regularApps:
            db_process = self.session.query(Process).filter_by(
                name=app.localizedName()).scalar()
            if not db_process:
                process_to_add = Process(app.localizedName())
                self.session.add(process_to_add)
                self.trycommit()
                db_process = self.session.query(Process).filter_by(
                    name=app.localizedName()).scalar()
            process_id = db_process.id

            if app not in self.current_apps:
                process_event = ProcessEvent(process_id, "Open")
                self.session.add(process_event)
                self.current_apps.append(app)

        # find apps that have closed since last time
        for app in self.current_apps:
            if app not in regularApps:
                db_process = self.session.query(Process).filter_by(
                    name=app.localizedName()).scalar()
                process_id = db_process.id
                process_event = ProcessEvent(process_id, "Close")
                self.session.add(process_event)
                self.trycommit()
                try:
                    self.current_apps.remove(app)
                except ValueError:
                    print(
                        "Error: Can not remove app from list. It does not seem to exist."
                    )

        for window in regularWindows:
            # get id of process in database
            process = self.session.query(Process).filter_by(
                name=window['process']).scalar()
            pid = process.id if process else 0
            geometry = window['geometry']

            # add new windows and tabs to the database
            db_window = self.session.query(Window).filter_by(
                title=window['title'],
                process_id=pid,
                browser_url=window['url']).scalar()
            if not db_window:
                window_to_add = Window(window['title'], pid, window['url'])
                self.session.add(window_to_add)
                self.trycommit()
                db_window = self.session.query(Window).filter_by(
                    title=window['title'],
                    process_id=pid,
                    browser_url=window['url']).scalar()
            window_id = db_window.id
            self.regularWindowsIds.append(window_id)

            if window_id not in self.current_windows:
                window_event = WindowEvent(window_id, "Open")
                self.session.add(window_event)
                self.trycommit()
                self.current_windows.append(window_id)

            # add new geometries to the database
            db_geometry = self.session.query(Geometry).filter_by(
                xpos=geometry['X'],
                ypos=geometry['Y'],
                width=geometry['Width'],
                height=geometry['Height']).scalar()
            if not db_geometry:
                geometry_to_add = Geometry(geometry['X'], geometry['Y'],
                                           geometry['Width'],
                                           geometry['Height'])
                self.session.add(geometry_to_add)
                self.trycommit()
                db_geometry = self.session.query(Geometry).filter_by(
                    xpos=geometry['X'],
                    ypos=geometry['Y'],
                    width=geometry['Width'],
                    height=geometry['Height']).scalar()
            geometry_id = db_geometry.id

        for window_id in self.current_windows:
            if window_id not in self.regularWindowsIds:
                window_event = WindowEvent(window_id, "Close")
                self.session.add(window_event)
                self.trycommit()
                try:
                    self.current_windows.remove(window_id)
                except ValueError:
                    print(
                        "Error: Can not remove window_id from list. It does not seem to exist."
                    )

        # check for current process, window, and geometry
        # if any of these are new, update currents, store keys, and take screenshot
        # if the event happened on a new process or window, update the current window

        #TODO this part is still buggy for newtabs and window events
        # may need to rename variables and not overwrite
        windows_to_ignore = ["Focus Proxy", "Clipboard"]
        if window_name not in windows_to_ignore:
            cur_process = self.session.query(Process).filter_by(
                name=process_name).scalar()
            if not cur_process:
                cur_process = Process(process_name)
                self.session.add(cur_process)
                self.trycommit()
                cur_process = self.session.query(Process).filter_by(
                    name=process_name).scalar()

            if cur_process.name != self.active_app['name']:

                if self.active_app['id'] != '':
                    process_event = ProcessEvent(self.active_app['id'],
                                                 "Inactive")
                    self.session.add(process_event)
                    self.trycommit()

                process_event = ProcessEvent(cur_process.id, "Active")
                self.session.add(process_event)
                self.active_app = {
                    'id': cur_process.id,
                    'name': cur_process.name
                }

            if browser_url == "NO_URL":
                cur_window = self.session.query(Window).filter_by(
                    title=window_name, process_id=cur_process.id).scalar()
            else:
                cur_window = self.session.query(Window).filter_by(
                    process_id=cur_process.id,
                    title=window_name,
                    browser_url=browser_url).scalar()
            if not cur_window:
                cur_window = Window(window_name, cur_process.id, browser_url)
                self.session.add(cur_window)
            if (cur_window.title != self.active_window['title']
                    or cur_window.process_id != self.active_window['process']
                    or cur_window.browser_url != self.active_window['url']):

                # We record that the old window is now inactive
                if self.active_window:
                    if (self.active_window['id'] != ''):
                        window_event = WindowEvent(self.active_window['id'],
                                                   "Inactive")
                        self.session.add(window_event)
                        self.trycommit()
# else :
# We should make sure no Open or Active window is left without a closing one.

                # We add the new window to the DB as Active
                window_event = WindowEvent(cur_window.id, "Active")
                self.session.add(window_event)
                self.trycommit()
                self.active_window = {
                    'id': cur_window.id,
                    'title': window_name,
                    'process': cur_process.id,
                    'url': browser_url
                }

            cur_geometry = self.session.query(Geometry).filter_by(
                xpos=win_x, ypos=win_y, width=win_width,
                height=win_height).scalar()
            if not cur_geometry:
                cur_geometry = Geometry(win_x, win_y, win_width, win_height)
                self.session.add(cur_geometry)

            # if its a new window, commit changes and update ids
            if (self.current_window.proc_id != cur_process.id
                    or self.current_window.win_id != cur_window.id):
                self.trycommit()
                self.store_keys(
                )  # happens before as these keypresses belong to the previous window
                self.current_window.proc_id = cur_process.id
                self.current_window.win_id = cur_window.id
                self.current_window.geo_id = cur_geometry.id
                self.take_screenshot()