KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_git_remotes.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
22#include <memory>
23
24#include <wx/datetime.h>
25
26#include <confirm.h>
30#include <eda_base_frame.h>
32#include <project.h>
33
34
36 PANEL_GIT_REMOTES_BASE( aParent ),
37 m_dialog( aDialog ),
38 m_frame( aFrame )
39{
40 m_remotesList->AppendColumn( _( "Name" ), wxLIST_FORMAT_LEFT, FromDIP( 110 ) );
41 m_remotesList->AppendColumn( _( "URL" ), wxLIST_FORMAT_LEFT, FromDIP( 340 ) );
42
44
46
47 // The detail pane appears once a remote is selected.
48 m_remoteSashPos = FromDIP( 460 );
50
51 m_remotesList->Bind( wxEVT_LIST_ITEM_SELECTED,
52 [this]( wxListEvent& aEvent )
53 {
54 if( !m_fillingRemotes )
55 showRemoteDetail( m_remotesList->GetItemText( aEvent.GetIndex(), 0 ) );
56 } );
57
58 m_remotesList->Bind( wxEVT_LIST_ITEM_DESELECTED,
59 [this]( wxListEvent& )
60 {
62 && m_remotesList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ) == -1 )
63 {
65 }
66 } );
67
68 m_editUrlButton->Bind( wxEVT_BUTTON,
69 [this]( wxCommandEvent& )
70 {
72 } );
73 m_removeRemoteButton->Bind( wxEVT_BUTTON,
74 [this]( wxCommandEvent& )
75 {
77 } );
78 m_addRemoteButton->Bind( wxEVT_BUTTON,
79 [this]( wxCommandEvent& )
80 {
82 } );
83}
84
85
87{
88 if( !m_dialog->SessionStillCurrent() )
89 return;
90
91 struct OUTCOME
92 {
93 std::vector<std::pair<wxString, wxString>> remotes;
94 wxDateTime fetched;
95 };
96
97 auto outcome = std::make_shared<OUTCOME>();
98
99 m_listQuery.Run(
100 m_frame->Prj().GetGitCommon(),
101 [outcome]( KIGIT_COMMON& aCommon )
102 {
103 outcome->remotes = aCommon.GetRemotes();
104 outcome->fetched = aCommon.GetLastFetchTime();
105 },
106 [this, outcome]()
107 {
108 if( !m_dialog->SessionStillCurrent() )
109 return;
110
111 wxString selected = KIGIT::SelectedText( m_remotesList, 0 );
112 long selectRow = wxNOT_FOUND;
113
114 m_fillingRemotes = true;
115 m_remotesList->DeleteAllItems();
116 m_lastFetched = outcome->fetched;
117
118 for( const auto& [name, url] : outcome->remotes )
119 {
120 long idx = m_remotesList->InsertItem( m_remotesList->GetItemCount(), name );
121 m_remotesList->SetItem( idx, 1, url );
122
123 // Keep the selection where it was, or start on the first remote.
124 if( name == selected || ( selected.IsEmpty() && idx == 0 ) )
125 selectRow = idx;
126 }
127
128 int shown = m_remotesList->GetItemCount();
129 m_remoteCount->SetLabel( wxString::Format( wxPLURAL( "%d remote", "%d remotes", shown ), shown ) );
130
131 if( selectRow != wxNOT_FOUND )
132 {
133 m_remotesList->SetItemState( selectRow, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
134 showRemoteDetail( m_remotesList->GetItemText( selectRow, 0 ) );
135 }
136 else
137 {
139 }
140
141 m_fillingRemotes = false;
142 } );
143}
144
145
146void PANEL_GIT_REMOTES::showRemoteDetail( const wxString& aRemote )
147{
148 if( aRemote.IsEmpty() )
149 {
151 return;
152 }
153
154 // FETCH_HEAD belongs to the repository, so every remote shares the listing's timestamp.
156}
157
158
159void PANEL_GIT_REMOTES::fillRemoteDetail( const wxString& aRemote, const wxDateTime& aFetched )
160{
161 // The list already holds the address.
162 wxString url;
163
164 for( long row = 0; row < m_remotesList->GetItemCount(); ++row )
165 {
166 if( m_remotesList->GetItemText( row, 0 ) == aRemote )
167 url = m_remotesList->GetItemText( row, 1 );
168 }
169
170 m_remoteTitle->SetLabel( wxString::Format( _( "Remote: %s" ), aRemote ) );
171 m_remoteUrl->ChangeValue( url );
172
173 // Git records fetches in FETCH_HEAD and nothing about pulls or pushes, so only that one
174 // can be shown.
175 m_remoteFetched->SetLabel( aFetched.IsValid()
176 ? wxString::Format( _( "Last fetch: %s" ), KIGIT::RelativeDate( aFetched ) )
177 : _( "Not fetched yet." ) );
178
179 if( !m_remotesSplitter->IsSplit() )
181
182 m_remoteDetail->Layout();
183}
184
185
190
191
192void PANEL_GIT_REMOTES::runRemoteAction( const std::function<bool( KIGIT_COMMON& )>& aMutation, const wxString& aError )
193{
194 m_dialog->RunGitAction(
195 [&aMutation]( KIGIT_COMMON& aCommon )
196 {
197 if( !aMutation( aCommon ) )
198 return false;
199
200 // Refresh the cached URL and credentials, so a later push does not reach the new
201 // remote authenticating as the old one.
202 aCommon.UpdateCurrentBranchInfo();
203 return true;
204 },
205 aError );
206}
207
208
210{
211 if( !m_dialog->SessionStillCurrent() )
212 return;
213
214 DIALOG_GIT_REMOTE dlg( this );
215
216 if( dlg.ShowModal() != wxID_OK )
217 return;
218
219 wxString name = dlg.GetRemoteName();
220 wxString url = dlg.GetUrl();
221
223 [name, url]( KIGIT_COMMON& aCommon )
224 {
225 return aCommon.AddRemote( name, url );
226 },
227 wxString::Format( _( "Could not add remote '%s'." ), name ) );
228}
229
230
232{
233 if( !m_dialog->SessionStillCurrent() )
234 return;
235
236 wxString name = KIGIT::SelectedText( m_remotesList, 0 );
237
238 if( name.IsEmpty() )
239 return;
240
241 wxString current = KIGIT::SelectedText( m_remotesList, 1 );
242 DIALOG_GIT_REMOTE dlg( this, name, current );
243
244 if( dlg.ShowModal() != wxID_OK )
245 return;
246
247 wxString url = dlg.GetUrl();
248
249 // The list shows the address with any user:password removed, so writing it back unchanged
250 // would delete credentials the config still holds.
251 if( url == current )
252 return;
253
255 [name, url]( KIGIT_COMMON& aCommon )
256 {
257 return aCommon.SetRemoteUrl( name, url );
258 },
259 wxString::Format( _( "Could not update remote '%s'." ), name ) );
260}
261
262
264{
265 if( !m_dialog->SessionStillCurrent() )
266 return;
267
268 wxString name = KIGIT::SelectedText( m_remotesList, 0 );
269
270 if( name.IsEmpty() )
271 return;
272
273 if( !IsOK( this, wxString::Format( _( "Remove remote '%s'?" ), name ) ) )
274 return;
275
277 [name]( KIGIT_COMMON& aCommon )
278 {
279 return aCommon.RemoveRemote( name );
280 },
281 wxString::Format( _( "Could not remove remote '%s'." ), name ) );
282}
const char * name
Ask for a remote's name and URL, for adding one or for editing the URL of one that exists.
wxString GetRemoteName() const
wxString GetUrl() const
int ShowModal() override
The base frame for deriving all KiCad main window classes.
The advanced Git window: a non-modal dialog with a top bar naming the repository and branch and a sid...
The project's git session.
bool RemoveRemote(const wxString &aName)
bool AddRemote(const wxString &aName, const wxString &aUrl)
bool SetRemoteUrl(const wxString &aName, const wxString &aUrl)
WX_SPLITTER_WINDOW * m_remotesSplitter
PANEL_GIT_REMOTES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
GIT_ADVANCED_DIALOG * m_dialog
PANEL_GIT_REMOTES(wxWindow *aParent, GIT_ADVANCED_DIALOG *aDialog, EDA_BASE_FRAME *aFrame)
KIGIT_QUERY m_listQuery
The list and its shared fetch timestamp are read together on a worker.
bool m_fillingRemotes
True while the list is being refilled, when its selection events are its own.
int m_remoteSashPos
Where the user left the sash between the list and its detail pane.
EDA_BASE_FRAME * m_frame
void Populate()
Fill the list from the repository's remotes. The repository is read on a worker.
void fillRemoteDetail(const wxString &aRemote, const wxDateTime &aFetched)
void runRemoteAction(const std::function< bool(KIGIT_COMMON &)> &aMutation, const wxString &aError)
Run a change to the remotes, then refresh the cached remote so a later push uses it.
void showRemoteDetail(const wxString &aRemote)
Fill the detail pane with aRemote, or hide it when the name is empty.
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:274
This file is part of the common library.
#define _(s)
Base window classes and related definitions.
void UnsplitKeepingSash(wxSplitterWindow *aSplitter, wxWindow *aPane, int &aSashPos)
Close a splitter's second pane, keeping the sash where the user left it for next time.
wxString SelectedText(wxListCtrl *aList, int aColumn)
The text of the selected row in a report list, or empty when nothing is selected.
wxString RelativeDate(const wxDateTime &aWhen)
"just now", "3 hours ago", or the ISO date once it is more than a month old.
void KeepDetailWidth(WX_SPLITTER_WINDOW *aSplitter)
Keep a split detail pane at the width its controls need, against both a narrow window and a dragged s...
void MarkDestructive(wxButton *aButton)
Paint a button's text the diff's red, for one that throws something away.