1 /**************************************************************************
2 ** This file is part of LiteIDE
3 **
4 ** Copyright (c) 2011-2019 LiteIDE. All rights reserved.
5 **
6 ** This library is free software; you can redistribute it and/or
7 ** modify it under the terms of the GNU Lesser General Public
8 ** License as published by the Free Software Foundation; either
9 ** version 2.1 of the License, or (at your option) any later version.
10 **
11 ** This library 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 GNU
14 ** Lesser General Public License for more details.
15 **
16 ** In addition, as a special exception,  that plugins developed for LiteIDE,
17 ** are allowed to remain closed sourced and can be distributed under any license .
18 ** These rights are included in the file LGPL_EXCEPTION.txt in this package.
19 **
20 **************************************************************************/
21 // Module: golangpackageoption.cpp
22 // Creator: visualfc <visualfc@gmail.com>
23 
24 #include "golangpackageoption.h"
25 #include "ui_golangpackageoption.h"
26 #include "golangpackage_global.h"
27 #include "liteenvapi/liteenvapi.h"
28 #include "gotool.h"
29 #include <QFileDialog>
30 #include <QLineEdit>
31 //lite_memory_check_begin
32 #if defined(WIN32) && defined(_MSC_VER) &&  defined(_DEBUG)
33      #define _CRTDBG_MAP_ALLOC
34      #include <stdlib.h>
35      #include <crtdbg.h>
36      #define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
37      #define new DEBUG_NEW
38 #endif
39 //lite_memory_check_end
40 
41 
GolangPackageOption(LiteApi::IApplication * app,QObject * parent)42 GolangPackageOption::GolangPackageOption(LiteApi::IApplication *app, QObject *parent) :
43     LiteApi::IOption(parent),
44     m_liteApp(app),
45     ui(new Ui::GolangPackageOption),
46     m_widget(new QWidget)
47 {
48     ui->setupUi(m_widget);
49     ui->cmbGoModule->addItems(QStringList() << "auto" << "on" << "off");
50     ui->cmbGoModule->setCurrentIndex(0);
51     ui->cmbGoModule->setEnabled(false);
52     ui->cmbGoProxy->setEnabled(false);
53     ui->editGoPrivate->setEnabled(false);
54     ui->editGoNoProxy->setEnabled(false);
55     ui->editGoNoSumdb->setEnabled(false);
56 
57     ui->cmbGoProxy->addItems(QStringList()
58                             << "https://proxy.golang.org,direct"
59                             << "https://goproxy.io"
60                             << "https://goproxy.io,direct"
61                             << "https://goproxy.cn"
62                             << "https://goproxy.cn,direct");
63     connect(ui->browserButton,SIGNAL(clicked()),this,SLOT(browser()));
64     connect(ui->clearButton,SIGNAL(clicked()),ui->litePathTextEdit,SLOT(clear()));
65     connect(ui->chkUseSysGopath,SIGNAL(toggled(bool)),ui->sysPathTextEdit,SLOT(setEnabled(bool)));
66     connect(ui->chkUseLiteGopath,SIGNAL(toggled(bool)),ui->litePathTextEdit,SLOT(setEnabled(bool)));
67     connect(ui->chkUseGoModule,SIGNAL(toggled(bool)),ui->cmbGoModule,SLOT(setEnabled(bool)));
68     connect(ui->chkUseGoProxy,SIGNAL(toggled(bool)),ui->cmbGoProxy,SLOT(setEnabled(bool)));
69     connect(ui->chkUseGoPrivate,SIGNAL(toggled(bool)),ui->editGoPrivate,SLOT(setEnabled(bool)));
70     connect(ui->chkUseGoNoProxy,SIGNAL(toggled(bool)),ui->editGoNoProxy,SLOT(setEnabled(bool)));
71     connect(ui->chkUseGoNoSumdb,SIGNAL(toggled(bool)),ui->editGoNoSumdb,SLOT(setEnabled(bool)));
72 }
73 
~GolangPackageOption()74 GolangPackageOption::~GolangPackageOption()
75 {
76     delete ui;
77 }
78 
79 
setSysPathList(const QStringList & pathList)80 void GolangPackageOption::setSysPathList(const QStringList &pathList)
81 {
82     ui->sysPathTextEdit->clear();
83     foreach (QString path, pathList) {
84         ui->sysPathTextEdit->appendPlainText(path);
85     }
86 }
87 
setLitePathList(const QStringList & pathList)88 void GolangPackageOption::setLitePathList(const QStringList &pathList)
89 {
90     ui->litePathTextEdit->clear();
91     foreach (QString path, pathList) {
92         ui->litePathTextEdit->appendPlainText(path);
93     }
94 }
95 
setUseSysGopath(bool b)96 void GolangPackageOption::setUseSysGopath(bool b)
97 {
98     ui->chkUseSysGopath->setChecked(b);
99 }
100 
isUseSysGopath() const101 bool GolangPackageOption::isUseSysGopath() const
102 {
103     return ui->chkUseSysGopath->isChecked();
104 }
105 
setUseLiteGopath(bool b)106 void GolangPackageOption::setUseLiteGopath(bool b)
107 {
108     ui->chkUseLiteGopath->setChecked(b);
109 }
110 
isUseLiteGopath() const111 bool GolangPackageOption::isUseLiteGopath() const
112 {
113     return ui->chkUseLiteGopath->isChecked();
114 }
115 
setUseGoModule(bool b)116 void GolangPackageOption::setUseGoModule(bool b)
117 {
118     ui->chkUseGoModule->setChecked(b);
119 }
120 
isUseGoModule() const121 bool GolangPackageOption::isUseGoModule() const
122 {
123     return ui->chkUseGoModule->isChecked();
124 }
125 
setSysGoModuleInfo(const QString & value)126 void GolangPackageOption::setSysGoModuleInfo(const QString &value)
127 {
128     ui->sysGoModuleInfo->setText(value);
129 }
130 
setGo111Module(const QString & value)131 void GolangPackageOption::setGo111Module(const QString &value)
132 {
133     for (int i = 0; i < ui->cmbGoModule->count(); i++) {
134         if (ui->cmbGoModule->itemText(i) == value) {
135             ui->cmbGoModule->setCurrentIndex(i);
136             break;
137         }
138     }
139 }
140 
go111Module() const141 QString GolangPackageOption::go111Module() const
142 {
143     return ui->cmbGoModule->currentText();
144 }
145 
litePathList() const146 QStringList GolangPackageOption::litePathList() const
147 {
148     return ui->litePathTextEdit->toPlainText().split("\n",QString::SkipEmptyParts);
149 }
150 
setUseGoProxy(bool b)151 void GolangPackageOption::setUseGoProxy(bool b)
152 {
153     ui->chkUseGoProxy->setChecked(b);
154 }
155 
isUseGoProxy() const156 bool GolangPackageOption::isUseGoProxy() const
157 {
158     return ui->chkUseGoProxy->isChecked();
159 }
160 
setGoProxy(const QString & v)161 void GolangPackageOption::setGoProxy(const QString &v)
162 {
163     ui->cmbGoProxy->lineEdit()->setText(v);
164 }
165 
goProxy() const166 QString GolangPackageOption::goProxy() const
167 {
168     return ui->cmbGoProxy->currentText();
169 }
170 
setUseGoPrivate(ENUM_GO_PRIVATE id,bool b)171 void GolangPackageOption::setUseGoPrivate(ENUM_GO_PRIVATE id, bool b)
172 {
173     switch (id) {
174     case GO_PRIVATE:
175         ui->chkUseGoPrivate->setChecked(b);
176         break;
177     case GO_NOPROXY:
178         ui->chkUseGoNoProxy->setChecked(b);
179         break;
180     case GO_NOSUMDB:
181         ui->chkUseGoNoSumdb->setChecked(b);
182         break;
183     }
184 }
185 
isUseGoPrivate(ENUM_GO_PRIVATE id) const186 bool GolangPackageOption::isUseGoPrivate(ENUM_GO_PRIVATE id) const
187 {
188     switch (id) {
189     case GO_PRIVATE:
190         return  ui->chkUseGoPrivate->isChecked();
191     case GO_NOPROXY:
192         return  ui->chkUseGoNoProxy->isChecked();
193     case GO_NOSUMDB:
194         return  ui->chkUseGoNoSumdb->isChecked();
195     }
196     return  false;
197 }
198 
setGoPrivate(ENUM_GO_PRIVATE id,const QString & value)199 void GolangPackageOption::setGoPrivate(ENUM_GO_PRIVATE id, const QString &value)
200 {
201     switch (id) {
202     case GO_PRIVATE:
203         ui->editGoPrivate->setText(value);
204         break;
205     case GO_NOPROXY:
206         ui->editGoNoProxy->setText(value);
207         break;
208     case GO_NOSUMDB:
209         ui->editGoNoSumdb->setText(value);
210         break;
211     }
212 }
213 
goPrivate(ENUM_GO_PRIVATE id) const214 QString GolangPackageOption::goPrivate(ENUM_GO_PRIVATE id) const
215 {
216     switch (id) {
217     case GO_PRIVATE:
218         return  ui->editGoPrivate->text();
219     case GO_NOPROXY:
220         return  ui->editGoNoProxy->text();
221     case GO_NOSUMDB:
222         return  ui->editGoNoSumdb->text();
223     }
224     return  QString();
225 }
226 
name() const227 QString GolangPackageOption::name() const
228 {
229     return "GolangPackage";
230 }
231 
mimeType() const232 QString GolangPackageOption::mimeType() const
233 {
234     return "option/golangpackage";
235 }
236 
save()237 void GolangPackageOption::save()
238 {
239     QStringList newLitePath = this->litePathList();
240     //m_liteApp->sendBroadcast("golangpackage","reloadgopath");
241     m_liteApp->settings()->setValue(LITEIDE_USESYSGOPATH,this->isUseSysGopath());
242     m_liteApp->settings()->setValue(LITEIDE_USELITEIDEGOPATH,this->isUseLiteGopath());
243     m_liteApp->settings()->setValue(LITEIDE_CUSTOMGO111MODULE,this->isUseGoModule());
244     m_liteApp->settings()->setValue(LITEIDE_GO111MODULE,this->go111Module());
245 
246     m_liteApp->settings()->setValue(LITEIDE_USEGOPROXY,this->isUseGoProxy());
247     m_liteApp->settings()->setValue(LITEIDE_GOPROXY,this->goProxy());
248 
249     m_liteApp->settings()->setValue(LITEIDE_USEGOPRIVATE,this->isUseGoPrivate(GO_PRIVATE));
250     m_liteApp->settings()->setValue(LITEIDE_GOPRIVATE,this->goPrivate(GO_PRIVATE));
251 
252     m_liteApp->settings()->setValue(LITEIDE_USEGONOPROXY,this->isUseGoPrivate(GO_NOPROXY));
253     m_liteApp->settings()->setValue(LITEIDE_GONOPROXY,this->goPrivate(GO_NOPROXY));
254 
255     m_liteApp->settings()->setValue(LITEIDE_USEGONOSUMDB,this->isUseGoPrivate(GO_NOSUMDB));
256     m_liteApp->settings()->setValue(LITEIDE_GONOSUMDB,this->goPrivate(GO_NOSUMDB));
257     //if (!hasSameList(orgLitePath,newLitePath)) {
258     setLiteGopath(m_liteApp,newLitePath);
259 //    this->reloadAll();
260     LiteApi::IEnvManager *env = LiteApi::getEnvManager(m_liteApp);
261     if (env) {
262         env->reloadCurrentEnv();
263     }
264 }
265 
load()266 void GolangPackageOption::load()
267 {
268     this->setSysPathList(sysGopath(m_liteApp));
269     this->setLitePathList(liteGopath(m_liteApp));
270     this->setUseSysGopath(m_liteApp->settings()->value(LITEIDE_USESYSGOPATH,true).toBool());
271     this->setUseLiteGopath(m_liteApp->settings()->value(LITEIDE_USELITEIDEGOPATH,true).toBool());
272     this->setUseGoModule(m_liteApp->settings()->value(LITEIDE_CUSTOMGO111MODULE,false).toBool());
273     this->setGo111Module(m_liteApp->settings()->value(LITEIDE_GO111MODULE,"auto").toString());
274     this->setUseGoProxy(m_liteApp->settings()->value(LITEIDE_USEGOPROXY,false).toBool());
275     this->setGoProxy(m_liteApp->settings()->value(LITEIDE_GOPROXY,"").toString());
276 
277     this->setUseGoPrivate(GO_PRIVATE,m_liteApp->settings()->value(LITEIDE_USEGOPRIVATE,false).toBool());
278     this->setGoPrivate(GO_PRIVATE,m_liteApp->settings()->value(LITEIDE_GOPRIVATE,"").toString());
279 
280     this->setUseGoPrivate(GO_NOPROXY,m_liteApp->settings()->value(LITEIDE_USEGONOPROXY,false).toBool());
281     this->setGoPrivate(GO_NOPROXY,m_liteApp->settings()->value(LITEIDE_GONOPROXY,"").toString());
282 
283     this->setUseGoPrivate(GO_NOSUMDB,m_liteApp->settings()->value(LITEIDE_USEGONOSUMDB,false).toBool());
284     this->setGoPrivate(GO_NOSUMDB,m_liteApp->settings()->value(LITEIDE_GONOSUMDB,"").toString());
285 
286     QProcessEnvironment env = LiteApi::getCurrentEnvironment(m_liteApp);
287     QString info = env.value("GO111MODULE");
288     if (!info.isEmpty()) {
289         this->setSysGoModuleInfo(QString("system GO111MODULE=%1").arg(info));
290     } else {
291         this->setSysGoModuleInfo("system GO111MODULE unset");
292     }
293 
294 }
295 
widget()296 QWidget *GolangPackageOption::widget()
297 {
298     return m_widget;
299 }
300 
browser()301 void GolangPackageOption::browser()
302 {
303     static QString last = QDir::homePath();
304     QString dir = QFileDialog::getExistingDirectory(m_widget, tr("Choose directory to add to GOPATH:"),
305                                                     last,
306                                                     QFileDialog::ShowDirsOnly
307                                                     | QFileDialog::DontResolveSymlinks);
308     if (!dir.isEmpty()) {
309         last = dir;
310         ui->litePathTextEdit->appendPlainText(dir);
311     }
312 }
313