roster/src/project_manager.py
Pavel Baksy 3cc96a82d6 Add foldable sidebar with custom project icons
Implement a collapsible sidebar that can be folded to show just project
icons in a slim strip, or expanded to show full project details. Projects
can now have custom icons selected from a 6x6 grid of 36 symbolic icons.

Features:
- Foldable sidebar with toggle button (fold/unfold)
- Slim sidebar view showing only project icons when folded
- Custom project icons with 36 symbolic icon choices
- Icon picker dialog with 6x6 grid layout
- Edit Project dialog (renamed from Rename) with name and icon selection
- Project icons displayed in both full and slim sidebar views
- Smooth transitions between folded/unfolded states
- Click on slim project icon to unfold sidebar

Technical changes:
- Add icon field to Project model with default "folder-symbolic"
- Create constants.py with PROJECT_ICONS list (36 symbolic icons)
- Implement IconPickerDialog with grid layout and selection
- Create SlimProjectItem widget for folded sidebar view
- Update ProjectManager.update_project() to handle icon changes
- Restructure sidebar using GtkStack for full/slim view switching
- Update project-item.ui to display project icon
- Change "Rename" menu to "Edit Project" with icon picker
- Add fold/unfold buttons with sidebar-show icons
- Update build system with new files and resources
2025-12-18 15:37:29 +01:00

123 lines
4.2 KiB
Python

# project_manager.py
#
# Copyright 2025 Pavel Baksy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
import json
import uuid
from pathlib import Path
from typing import List
from datetime import datetime, timezone
from .models import Project, SavedRequest, HttpRequest
class ProjectManager:
"""Manages project and saved request persistence."""
def __init__(self):
# Store in ~/.roster/ (not ~/.config/) for git versioning
self.data_dir = Path.home() / '.roster'
self.projects_file = self.data_dir / 'requests.json'
self._ensure_data_dir()
def _ensure_data_dir(self):
"""Create data directory if it doesn't exist."""
self.data_dir.mkdir(parents=True, exist_ok=True)
def load_projects(self) -> List[Project]:
"""Load projects from JSON file."""
if not self.projects_file.exists():
return []
try:
with open(self.projects_file, 'r') as f:
data = json.load(f)
return [Project.from_dict(p) for p in data.get('projects', [])]
except Exception as e:
print(f"Error loading projects: {e}")
return []
def save_projects(self, projects: List[Project]):
"""Save projects to JSON file."""
try:
data = {
'version': 1,
'projects': [p.to_dict() for p in projects]
}
with open(self.projects_file, 'w') as f:
json.dump(data, f, indent=2)
except Exception as e:
print(f"Error saving projects: {e}")
def add_project(self, name: str) -> Project:
"""Create new project."""
projects = self.load_projects()
project = Project(
id=str(uuid.uuid4()),
name=name,
requests=[],
created_at=datetime.now(timezone.utc).isoformat()
)
projects.append(project)
self.save_projects(projects)
return project
def update_project(self, project_id: str, new_name: str = None, new_icon: str = None):
"""Update a project's name and/or icon."""
projects = self.load_projects()
for p in projects:
if p.id == project_id:
if new_name is not None:
p.name = new_name
if new_icon is not None:
p.icon = new_icon
break
self.save_projects(projects)
def delete_project(self, project_id: str):
"""Delete a project and all its requests."""
projects = self.load_projects()
projects = [p for p in projects if p.id != project_id]
self.save_projects(projects)
def add_request(self, project_id: str, name: str, request: HttpRequest) -> SavedRequest:
"""Add request to a project."""
projects = self.load_projects()
now = datetime.now(timezone.utc).isoformat()
saved_request = SavedRequest(
id=str(uuid.uuid4()),
name=name,
request=request,
created_at=now,
modified_at=now
)
for p in projects:
if p.id == project_id:
p.requests.append(saved_request)
break
self.save_projects(projects)
return saved_request
def delete_request(self, project_id: str, request_id: str):
"""Delete a saved request."""
projects = self.load_projects()
for p in projects:
if p.id == project_id:
p.requests = [r for r in p.requests if r.id != request_id]
break
self.save_projects(projects)