summaryrefslogtreecommitdiff
path: root/kodereviewer/models/project.py
blob: c676157badf86f4e90e9b4d15934024fe26e12fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
from os import path
from typing import Any

from PySide6.QtCore import QAbstractListModel, QByteArray, QModelIndex, QObject, QPersistentModelIndex, QStandardPaths, QUrl, Qt, Signal, Slot, Property
from PySide6.QtQml import QmlElement

from kodereviewer.project import Project

QML_IMPORT_NAME = "org.deprecated.kodereviewer"
QML_IMPORT_MAJOR_VERSION = 1


@QmlElement
class ProjectModel(QAbstractListModel):
    """Projects list!"""

    projects: list[Project]

    NameRole = Qt.ItemDataRole.UserRole + 1
    OwnerRole = NameRole + 1
    UrlRole = OwnerRole + 1

    def __init__(self):
        super().__init__()
        self.projects = []

        project_config = self._project_file()
        try:
            with open(project_config) as fp:
                data = json.load(fp)
                if isinstance(data, list):
                    self._load_projects(data)
        except OSError as e:
            pass

    def _project_file(self) -> str:
        app_data = QStandardPaths.writableLocation(QStandardPaths.AppDataLocation)
        project_config = path.join(app_data, 'projects.json')
        return project_config

    def data(self,
             index: QModelIndex | QPersistentModelIndex,
             role: int = Qt.ItemDataRole.DisplayRole) -> object:
        project = self.projects[index.row()]

        if role == Qt.ItemDataRole.DisplayRole:
            return project.name
        if role == self.NameRole:
            return project.name
        if role == self.OwnerRole:
            return project.owner
        if role == self.UrlRole:
            return project.url

        return None

    def rowCount(self, parent: QModelIndex | QPersistentModelIndex = QModelIndex()) -> int:
        return len(self.projects)

    def roleNames(self) -> dict[int, QByteArray]:
        return {
            self.NameRole: QByteArray(b"name"),
            self.OwnerRole: QByteArray(b"owner"),
            self.UrlRole: QByteArray(b"url")
        }

    @Slot(int, result=Project)
    def get(self, index: int) -> Project:
        return self.projects[index]

    def _load_projects(self, data: list[dict[str, Any]]) -> None:
        for project in data:
            self.projects.append(Project(
                project['name'],
                project['owner'],
                project['url']
            ))

    def _save_projects(self):
        project_config = self._project_file()
        data = [{'name': project.name, 'owner': project.owner, 'url': project.url} for project in self.projects]

        with open(self._project_file(), 'w') as fp:
            json.dump(data, fp)

    @Slot(str, str, str)
    def add(self, name: str, owner: str, url: str) -> None:
        self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
        self.projects.append(Project(name, owner, url))
        self._save_projects()
        self.endInsertRows()