Exemplo n.º 1
0
def create_messages_table_for_dialog(peer_id: str):
    SQLManager.get_instance(DB_MESSAGING).create_table(f"messages_{peer_id}", [
        "message_id", "timestamp", "text", "mine", "service", "from_peer_id",
        "from_nickname"
    ], [
        ColumnTypes.TEXT, ColumnTypes.NUMERIC, ColumnTypes.TEXT,
        ColumnTypes.INTEGER, ColumnTypes.INTEGER, ColumnTypes.TEXT,
        ColumnTypes.TEXT
    ])
Exemplo n.º 2
0
def get_settings_from_db() -> Settings:
    settings = Settings()
    for category in SQLManager.get_instance(DB_SETTINGS).select_all(
            "_categories"):
        for setting in SQLManager.get_instance(DB_SETTINGS).select_all(
                category[0]):
            settings.set(Category(category[0], category[1]),
                         Setting(setting[0], setting[1], setting[2],
                                 setting[3]),
                         init=True)

    return settings
Exemplo n.º 3
0
def save_message(peer_id: str,
                 message: Message,
                 from_peer_id: str,
                 service: bool = False,
                 from_nickname: str = ""):
    SQLManager.get_instance(DB_MESSAGING).add_record(f"messages_{peer_id}", [
        "message_id", "timestamp", "text", "mine", "service", "from_peer_id",
        "from_nickname"
    ], [
        message.message_id, message.timestamp,
        message.text.replace("'", "''"), 1 if message.mine else 0,
        1 if service else 0, from_peer_id, from_nickname
    ])
Exemplo n.º 4
0
def create_drafts_table():
    manager = SQLManager.get_instance(DB_MESSAGING)
    manager.create_table("drafts", ["peer_id", "draft"],
                         [ColumnTypes.TEXT, ColumnTypes.TEXT])

    for _, _, _, peer_id, _, _ in manager.select_all("dialogs"):
        manager.add_record("drafts", ["peer_id", "draft"], [peer_id, ""])
Exemplo n.º 5
0
def create_dialog(host: str, port: str, chat_type: int, peer_id: str):
    sql_manager = SQLManager.get_instance(DB_MESSAGING)
    sql_manager.add_record(
        "dialogs",
        ["host", "port", "chat_type", "peer_id", "nickname", "listening_port"],
        [host, port, chat_type, peer_id, "", ""])
    create_messages_table_for_dialog(peer_id)
    create_draft(peer_id)
Exemplo n.º 6
0
def save_storage_to_db(storage: Storage):
    sql_manager = SQLManager.get_instance(DB_STORAGE)

    for key, value in storage.iterate():
        if storage.is_new(key):
            sql_manager.add_record("storage", ["name", "value"], [key, value])
        else:
            sql_manager.edit_record(Query(["name"], [key]), "storage",
                                    ["name", "value"], [key, value])
Exemplo n.º 7
0
def create_category_table(category: Category):
    sql_manager = SQLManager.get_instance(DB_SETTINGS)
    if not sql_manager.has_table(category.name):
        sql_manager.add_record("_categories", ["name", "display_name"],
                               [category.name, category.display_name])

    sql_manager.create_table(category.name,
                             ["name", "value", "type", "display_name"], [
                                 ColumnTypes.TEXT, ColumnTypes.TEXT,
                                 ColumnTypes.NUMERIC, ColumnTypes.TEXT
                             ])
Exemplo n.º 8
0
def get_messages(peer_id):
    messages = SQLManager.get_instance(DB_MESSAGING).select_all(
        f"messages_{peer_id}")
    res = list()
    for msg in messages:
        res.append((Message(message_id=msg[0],
                            timestamp=msg[1],
                            text=msg[2],
                            mine=msg[3] == 1), bool(msg[4]), msg[5], msg[6]))

    return res
Exemplo n.º 9
0
def save_settings_to_db(settings: Settings):
    sql_manager = SQLManager.get_instance(DB_SETTINGS)

    for category, setting in settings.iterate():
        if settings.is_new_category(category):
            create_category_table(category)

        if settings.is_new(category, setting):
            sql_manager.add_record(
                category.name, ["name", "value", "type", "display_name"], [
                    setting.key, setting.value, setting.setting_type,
                    setting.display_name
                ])
        else:
            sql_manager.edit_record(Query(["name"], [setting.key]),
                                    category.name, ["name", "value"],
                                    [setting.key, setting.value])
Exemplo n.º 10
0
def edit_message(peer_id: str, message: Message):
    SQLManager.get_instance(DB_MESSAGING).edit_record(
        Query(["message_id"], [message.message_id]), f"messages_{peer_id}",
        ["text"], [message.text])
Exemplo n.º 11
0
def save_draft(peer_id: str, draft: str):
    SQLManager.get_instance(DB_MESSAGING) \
        .edit_record(Query(["peer_id"], [peer_id]),
                     "drafts",
                     ["draft"], [draft])
Exemplo n.º 12
0
def delete_dialog(peer_id: str):
    SQLManager.get_instance(DB_MESSAGING).delete_record(
        "dialogs", f"peer_id='{peer_id}'")
    delete_messages_table_for_dialog(peer_id)
Exemplo n.º 13
0
def update_dialog_info(nickname: str, port: int, peer_id: str):
    SQLManager.get_instance(DB_MESSAGING).edit_record(
        Query(["peer_id"], [peer_id]), "dialogs",
        ["nickname", "listening_port"], [nickname, port])
Exemplo n.º 14
0
def update_dialog(host: str, port: int, chat_type: int, peer_id: str):
    SQLManager.get_instance(DB_MESSAGING).edit_record(
        Query(["peer_id"], [peer_id]), "dialogs",
        ["host", "port", "chat_type"], [host, port, chat_type])
Exemplo n.º 15
0
def create_settings_table():
    sql_manager = SQLManager.get_instance(DB_SETTINGS)
    sql_manager.create_table("_categories", ["name", "display_name"],
                             [ColumnTypes.TEXT, ColumnTypes.TEXT])

    create_category_table(Category("general", "General"))
Exemplo n.º 16
0
def create_storage_table():
    SQLManager.get_instance(DB_STORAGE) \
        .create_table("storage",
                      ["name", "value"],
                      [ColumnTypes.TEXT, ColumnTypes.TEXT])
Exemplo n.º 17
0
def create_dialogs_table():
    SQLManager.get_instance(DB_MESSAGING) \
        .create_table("dialogs",
                      ["host", "port", "chat_type", "peer_id", "nickname", "listening_port"],
                      [ColumnTypes.TEXT, ColumnTypes.NUMERIC, ColumnTypes.NUMERIC, ColumnTypes.TEXT, ColumnTypes.TEXT, ColumnTypes.NUMERIC])
Exemplo n.º 18
0
def create_draft(peer_id: str):
    SQLManager.get_instance(DB_MESSAGING) \
        .add_record("drafts",
                    ["peer_id", "draft"],
                    [peer_id, ""])
Exemplo n.º 19
0
def delete_messages_table_for_dialog(peer_id: str):
    SQLManager.get_instance(DB_MESSAGING).delete_table(f"messages_{peer_id}")
Exemplo n.º 20
0
def get_draft(peer_id: str):
    return SQLManager.get_instance(DB_MESSAGING).select_single(
        "peer_id", peer_id, "drafts")[1]
Exemplo n.º 21
0
def delete_message(peer_id: str, message_id: str):
    SQLManager.get_instance(DB_MESSAGING).delete_record(
        f"messages_{peer_id}", f"message_id='{message_id}'")
Exemplo n.º 22
0
def get_storage_from_db() -> Storage:
    storage = Storage()
    for pref in SQLManager.get_instance(DB_STORAGE).select_all("storage"):
        storage.set(pref[0], pref[1], init=True)

    return storage
Exemplo n.º 23
0
    def init_ui(self):
        layout = QVBoxLayout()
        theme = ThemeLoader.loaded_theme

        # shortcuts setup
        close_dialog_shortcut = QShortcut(QKeySequence("Esc"), self)
        close_dialog_shortcut.activated.connect(
            lambda: self.close_dialog_event())
        close_dialog_shortcut.setEnabled(True)

        dialog_up_shortcut = QShortcut(QKeySequence("Alt+Up"), self)
        dialog_up_shortcut.activated.connect(lambda: self.open_dialog_above())
        dialog_up_shortcut.setEnabled(True)

        dialog_down_shortcut = QShortcut(QKeySequence("Alt+Down"), self)
        dialog_down_shortcut.activated.connect(
            lambda: self.open_dialog_below())
        dialog_down_shortcut.setEnabled(True)

        open_dialogs_shortcut = QShortcut(QKeySequence("Ctrl+Alt+D"), self)
        open_dialogs_shortcut.activated.connect(
            lambda: self.tabs.setCurrentIndex(0))
        open_dialogs_shortcut.setEnabled(True)

        open_incoming_shortcut = QShortcut(QKeySequence("Ctrl+Alt+I"), self)
        open_incoming_shortcut.activated.connect(
            lambda: self.tabs.setCurrentIndex(1))
        open_incoming_shortcut.setEnabled(True)

        accept_connection_shortcut = QShortcut(QKeySequence("Ctrl+Shift+A"),
                                               self)
        accept_connection_shortcut.activated.connect(
            lambda: self.accept_connection_event(self.incoming_list.
                                                 currentItem()))
        accept_connection_shortcut.setEnabled(True)

        decline_connection_shortcut = QShortcut(QKeySequence("Ctrl+Shift+D"),
                                                self)
        decline_connection_shortcut.activated.connect(
            lambda: self.decline_connection_event(self.incoming_list.
                                                  currentItem()))
        decline_connection_shortcut.setEnabled(True)

        # dialogs list setup
        self.dialogs_list.setStyleSheet(
            theme.apply_to_stylesheet(styles.side_list_style))
        self.dialogs_list.setIconSize(QSize(40, 40))
        self.dialogs_list.setSelectionMode(QAbstractItemView.SingleSelection)
        self.dialogs_list.currentItemChanged.connect(
            lambda current, previous: dialog_item_changed_callback(
                current, previous, main_window))
        self.dialogs_list.setSpacing(5)

        self.dialogs_list.setContextMenuPolicy(Qt.CustomContextMenu)
        self.dialogs_list.customContextMenuRequested.connect(
            self.dialog_context_menu_event)

        dialogs = SQLManager.get_instance(DB_MESSAGING).select_all("dialogs")
        for item in dialogs:
            self.dialogs_list.addItem(
                DialogItemWidget(item[4],
                                 item[0],
                                 item[1],
                                 item[2],
                                 peer_id=item[3]))

        # incoming list setup
        self.incoming_list.setStyleSheet(
            theme.apply_to_stylesheet(styles.side_list_style))
        self.incoming_list.setContextMenuPolicy(Qt.CustomContextMenu)
        self.incoming_list.customContextMenuRequested.connect(
            self.incoming_context_menu_event)

        # tabs setup
        self.tabs.setStyleSheet(theme.get_default_for_widget(self.tabs))
        self.tabs.addTab(self.dialogs_list, "Dialogs")
        self.tabs.addTab(self.incoming_list, "Incoming Connections")

        layout.addWidget(self.head)
        layout.addWidget(self.tabs)
        layout.setContentsMargins(0, 5, 0, 0)

        client_base.incoming_connection_callback = lambda address, connection: handle_incoming_connection_callback(
            self.incoming_list, address)

        self.setLayout(layout)