KiCad PCB EDA Suite
Loading...
Searching...
No Matches
local_history_pane.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "local_history_pane.h"
21#include "kicad_manager_frame.h"
22
23#include <git2.h>
24#include <project.h>
25#include <wx/filename.h>
26#include <wx/intl.h>
27#include <wx/menu.h>
28#include <wx/settings.h>
29
30wxDEFINE_EVENT( EVT_LOCAL_HISTORY_REFRESH, wxCommandEvent );
31
33 wxPanel( aParent ),
34 m_frame( aParent ),
35 m_list( nullptr ),
36 m_refreshTimer( this )
37{
38 m_list = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL );
39 m_list->AppendColumn( _( "Title" ) );
40 m_list->AppendColumn( _( "Time" ) );
41 m_list->SetColumnWidth( 0, FromDIP( 200 ) );
42 m_list->SetColumnWidth( 1, FromDIP( 150 ) );
43
44 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
45 sizer->Add( m_list, 1, wxEXPAND );
46 SetSizer( sizer );
47
48 m_list->Bind( wxEVT_MOTION, &LOCAL_HISTORY_PANE::OnMotion, this );
49 m_list->Bind( wxEVT_LIST_ITEM_RIGHT_CLICK, &LOCAL_HISTORY_PANE::OnRightClick, this );
50 Bind( wxEVT_TIMER, &LOCAL_HISTORY_PANE::OnRefreshTimer, this, m_refreshTimer.GetId() );
51 Bind( EVT_LOCAL_HISTORY_REFRESH, &LOCAL_HISTORY_PANE::OnRefreshEvent, this );
52
53 m_refreshTimer.Start( 10000 );
54}
55
60
61void LOCAL_HISTORY_PANE::RefreshHistory( const wxString& aProjectPath )
62{
63 if( !IsShownOnScreen() )
64 return;
65
66 m_list->DeleteAllItems();
67 m_commits.clear();
68
69 wxFileName hist( aProjectPath, wxS( ".history" ) );
70
71 git_repository* repo = nullptr;
72 if( git_repository_open( &repo, hist.GetFullPath().mb_str().data() ) != 0 )
73 return;
74
75 git_revwalk* walk = nullptr;
76 if( git_revwalk_new( &walk, repo ) != 0 )
77 {
78 git_repository_free( repo );
79 return;
80 }
81
82 if( git_revwalk_push_head( walk ) != 0 )
83 {
84 git_revwalk_free( walk );
85 git_repository_free( repo );
86 return;
87 }
88
89 wxDateTime now = wxDateTime::Now();
90
91 git_oid oid;
92 while( git_revwalk_next( &oid, walk ) == 0 )
93 {
94 git_commit* commit = nullptr;
95 if( git_commit_lookup( &commit, repo, &oid ) != 0 )
96 continue;
97
99 info.hash = wxString::FromUTF8( git_oid_tostr_s( &oid ) );
100 info.summary = wxString::FromUTF8( git_commit_summary( commit ) );
101 info.message = wxString::FromUTF8( git_commit_message( commit ) );
102 info.date = wxDateTime( static_cast<time_t>( git_commit_time( commit ) ) );
103
104 // Calculate relative time
105 wxTimeSpan elapsed = now - info.date;
106 wxString timeStr;
107
108 if( elapsed.GetMinutes() < 1 )
109 {
110 timeStr = _( "Moments ago" );
111 }
112 else if( elapsed.GetMinutes() < 91 )
113 {
114 int minutes = elapsed.GetMinutes();
115 timeStr = wxString::Format( _( "%d minutes ago" ), minutes );
116 }
117 else if( elapsed.GetHours() < 24 )
118 {
119 int hours = elapsed.GetHours();
120 timeStr = wxString::Format( _( "%d hours ago" ), hours );
121 }
122 else
123 {
124 timeStr = info.date.Format();
125 }
126
127 wxString title = info.message.BeforeFirst( '\n' );
128 long row = m_list->InsertItem( m_list->GetItemCount(), title );
129 m_list->SetItem( row, 1, timeStr );
130
131 // Tint by foreground colour so light and dark themes both stay readable.
132 if( info.summary.StartsWith( wxS( "Autosave" ) ) )
133 m_list->SetItemTextColour( row, wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
134 else if( info.summary.StartsWith( wxS( "Backup" ) ) )
135 m_list->SetItemTextColour( row, wxColour( 80, 120, 200 ) );
136
137 m_commits.emplace_back( std::move( info ) );
138 git_commit_free( commit );
139 }
140
141 git_revwalk_free( walk );
142 git_repository_free( repo );
143}
144
145void LOCAL_HISTORY_PANE::OnMotion( wxMouseEvent& aEvent )
146{
147 int flags = 0;
148 long item = m_list->HitTest( aEvent.GetPosition(), flags );
149
150 if( item >= 0 && item < static_cast<long>( m_commits.size() ) )
151 {
152 const LOCAL_COMMIT_INFO& info = m_commits[item];
153 wxString tip = info.message;
154
155 if( !tip.EndsWith( wxS( "\n" ) ) )
156 tip << wxS( "\n" );
157
158 tip << wxS( "\n" ) << info.date.FormatISOCombined();
159
160 if( m_list->GetToolTipText() != tip )
161 m_list->SetToolTip( tip );
162 }
163 else
164 {
165 m_list->UnsetToolTip();
166 }
167
168 aEvent.Skip();
169}
170
171void LOCAL_HISTORY_PANE::OnRightClick( wxListEvent& aEvent )
172{
173 long item = aEvent.GetIndex();
174
175 if( item < 0 || item >= static_cast<long>( m_commits.size() ) )
176 return;
177
178 wxMenu menu;
179 wxMenuItem* restore = menu.Append( wxID_ANY, _( "Restore Commit" ) );
180 menu.Bind( wxEVT_MENU,
181 [this, item]( wxCommandEvent& )
182 {
183 m_frame->RestoreCommitFromHistory( m_commits[item].hash );
184 },
185 restore->GetId() );
186 PopupMenu( &menu );
187}
188
189
190void LOCAL_HISTORY_PANE::OnRefreshEvent( wxCommandEvent& aEvent )
191{
192 wxString projectPath = aEvent.GetString();
193
194 if( projectPath.IsEmpty() )
195 projectPath = m_frame->Prj().GetProjectPath();
196
197 RefreshHistory( projectPath );
198}
199
200
201void LOCAL_HISTORY_PANE::OnRefreshTimer( wxTimerEvent& aEvent )
202{
203 // Refresh the history to show updates from autosave
204 RefreshHistory( m_frame->Prj().GetProjectPath() );
205}
The main KiCad project manager frame.
LOCAL_HISTORY_PANE(KICAD_MANAGER_FRAME *aParent)
void OnRefreshTimer(wxTimerEvent &aEvent)
void OnRightClick(wxListEvent &aEvent)
KICAD_MANAGER_FRAME * m_frame
void RefreshHistory(const wxString &aProjectPath)
void OnRefreshEvent(wxCommandEvent &aEvent)
std::vector< LOCAL_COMMIT_INFO > m_commits
void OnMotion(wxMouseEvent &aEvent)
#define _(s)
wxDEFINE_EVENT(EVT_LOCAL_HISTORY_REFRESH, wxCommandEvent)