KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_git_repos.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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "panel_git_repos.h"
21
22#include <bitmaps.h>
24#include <kiplatform/secrets.h>
25#include <pgm_base.h>
27#include <trace_helpers.h>
29#include <widgets/wx_grid.h>
30
31#include <git2.h>
33#include <wx/bmpbuttn.h>
34#include <wx/button.h>
35#include <wx/checkbox.h>
36#include <wx/log.h>
37
38
40{
41}
42
46
47
49{
50 m_cbDefault->SetValue( true );
51 m_author->SetValue( wxEmptyString );
52 m_authorEmail->SetValue( wxEmptyString );
53 m_cbShowAvatars->SetValue( false );
54}
55
56static std::pair<wxString, wxString> getDefaultAuthorAndEmail()
57{
58 wxString name;
59 wxString email;
60 git_config_entry* name_c = nullptr;
61 git_config_entry* email_c = nullptr;
62
63 git_config* config = nullptr;
64
65 if( git_config_open_default( &config ) != 0 )
66 {
67 wxLogTrace( traceGit, "Failed to open default Git config: %s", KIGIT_COMMON::GetLastGitError() );
68 return std::make_pair( name, email );
69 }
70
71 KIGIT::GitConfigPtr configPtr( config );
72
73 if( git_config_get_entry( &name_c, config, "user.name" ) != 0 )
74 {
75 wxLogTrace( traceGit, "Failed to get user.name from Git config: %s", KIGIT_COMMON::GetLastGitError() );
76 return std::make_pair( name, email );
77 }
78
79 KIGIT::GitConfigEntryPtr namePtr( name_c );
80
81 if( git_config_get_entry( &email_c, config, "user.email" ) != 0 )
82 {
83 wxLogTrace( traceGit, "Failed to get user.email from Git config: %s", KIGIT_COMMON::GetLastGitError() );
84 return std::make_pair( name, email );
85 }
86
87 KIGIT::GitConfigEntryPtr emailPtr( email_c );
88
89 if( name_c )
90 name = name_c->value;
91
92 if( email_c )
93 email = email_c->value;
94
95 return std::make_pair( name, email );
96}
97
98
100{
101 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
102
103 settings->m_Git.enableGit = m_enableGit->GetValue();
104 settings->m_Git.updatInterval = m_updateInterval->GetValue();
105 settings->m_Git.useDefaultAuthor = m_cbDefault->GetValue();
106 settings->m_Git.authorName = m_author->GetValue();
107 settings->m_Git.authorEmail = m_authorEmail->GetValue();
108 settings->m_Git.showContributorAvatars = m_cbShowAvatars->GetValue();
109
110 return true;
111}
112
113
115{
116 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
117 std::pair<wxString, wxString> defaultAuthor = getDefaultAuthorAndEmail();
118
119 m_enableGit->SetValue( settings->m_Git.enableGit );
120 m_updateInterval->SetValue( settings->m_Git.updatInterval );
121 m_cbShowAvatars->SetValue( settings->m_Git.showContributorAvatars );
122
123 m_cbDefault->SetValue( settings->m_Git.useDefaultAuthor );
124
125 if( settings->m_Git.useDefaultAuthor )
126 {
127 m_author->SetValue( defaultAuthor.first );
128 m_authorEmail->SetValue( defaultAuthor.second );
129 }
130 else
131 {
132 if( settings->m_Git.authorName.IsEmpty() )
133 m_author->SetValue( defaultAuthor.first );
134 else
135 m_author->SetValue( settings->m_Git.authorName );
136
137 if( settings->m_Git.authorEmail.IsEmpty() )
138 m_authorEmail->SetValue( defaultAuthor.second );
139 else
140 m_authorEmail->SetValue( settings->m_Git.authorEmail );
141 }
142
143 wxCommandEvent event;
144 onDefaultClick( event );
145 onEnableGitClick( event );
146 return true;
147}
148
149void PANEL_GIT_REPOS::onDefaultClick( wxCommandEvent& event )
150{
151 m_author->Enable( !m_cbDefault->GetValue() );
152 m_authorEmail->Enable( !m_cbDefault->GetValue() );
153 m_authorLabel->Enable( !m_cbDefault->GetValue() );
154 m_authorEmailLabel->Enable( !m_cbDefault->GetValue() );
155}
156
157void PANEL_GIT_REPOS::onEnableGitClick( wxCommandEvent& aEvent )
158{
159 bool enable = m_enableGit->GetValue();
160 m_updateInterval->Enable( enable );
161 m_cbShowAvatars->Enable( enable );
162 m_avatarsLabel->Enable( enable );
163 m_avatarsNote->Enable( enable );
164 m_cbDefault->Enable( enable );
165 m_author->Enable( enable && !m_cbDefault->GetValue() );
166 m_authorEmail->Enable( enable && !m_cbDefault->GetValue() );
167 m_authorLabel->Enable( enable && !m_cbDefault->GetValue() );
168 m_authorEmailLabel->Enable( enable && !m_cbDefault->GetValue() );
169}
const char * name
static wxString GetLastGitError()
libgit2's last message, without credentials from any URL it quotes, so it is safe to show or trace.
wxStaticText * m_authorEmailLabel
PANEL_GIT_REPOS_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)
void onEnableGitClick(wxCommandEvent &event) override
PANEL_GIT_REPOS(wxWindow *parent)
~PANEL_GIT_REPOS() override
void onDefaultClick(wxCommandEvent &event) override
void ResetPanel() override
Reset the contents of this panel.
bool TransferDataToWindow() override
bool TransferDataFromWindow() override
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:562
const wxChar *const traceGit
Flag to enable Git debugging output.
std::unique_ptr< git_config, decltype([](git_config *aConfig) { git_config_free(aConfig); })> GitConfigPtr
A unique pointer for git_config objects with automatic cleanup.
std::unique_ptr< git_config_entry, decltype([](git_config_entry *aEntry) { git_config_entry_free(aEntry); })> GitConfigEntryPtr
A unique pointer for git_config_entry objects with automatic cleanup.
static std::pair< wxString, wxString > getDefaultAuthorAndEmail()
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
wxLogTrace helper definitions.