-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLauncherWindow.cpp
More file actions
844 lines (708 loc) · 33.4 KB
/
Copy pathLauncherWindow.cpp
File metadata and controls
844 lines (708 loc) · 33.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/* LauncherWindow.cpp – HaiClaude (Linux/Qt6) */
#include "LauncherWindow.h"
#include <QButtonGroup>
#include <QCheckBox>
#include <QComboBox>
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QFormLayout>
#include <QGroupBox>
#include <QGuiApplication>
#include <QHBoxLayout>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QMessageBox>
#include <QProcess>
#include <QPushButton>
#include <QRadioButton>
#include <QScreen>
#include <QSettings>
#include <QStandardPaths>
#include <QTextEdit>
#include <QVBoxLayout>
#include <unistd.h>
// ---------------------------------------------------------------------------
// Helper: Shell Escape
// ---------------------------------------------------------------------------
static QString shellEscape(const QString& s)
{
return "'" + QString(s).replace("'", "'\\''") + "'";
}
// ---------------------------------------------------------------------------
// Constructor
// ---------------------------------------------------------------------------
LauncherWindow::LauncherWindow(QWidget* parent)
: QWidget(parent)
{
setWindowTitle("Claude Code Launcher");
// --- Mode radios (exclusive group: Cloud vs API) ---
fModeGroup = new QButtonGroup(this);
fCloudRadio = new QRadioButton("Cloud");
fApiRadio = new QRadioButton("API (API key)");
fModeGroup->addButton(fCloudRadio, 0);
fModeGroup->addButton(fApiRadio, 1);
fCloudRadio->setChecked(true);
// --- Cloud model radios (separate exclusive group: Opus/Sonnet/Haiku) ---
fModelGroup = new QButtonGroup(this);
fCloudOpusRadio = new QRadioButton("Opus");
fCloudSonnetRadio = new QRadioButton("Sonnet");
fCloudHaikuRadio = new QRadioButton("Haiku");
fModelGroup->addButton(fCloudOpusRadio, 0);
fModelGroup->addButton(fCloudSonnetRadio, 1);
fModelGroup->addButton(fCloudHaikuRadio, 2);
fCloudOpusRadio->setChecked(true);
fModelBox = new QGroupBox("Model");
QHBoxLayout* modelLayout = new QHBoxLayout(fModelBox);
modelLayout->addStretch();
modelLayout->addWidget(fCloudOpusRadio);
modelLayout->addWidget(fCloudSonnetRadio);
modelLayout->addWidget(fCloudHaikuRadio);
modelLayout->addStretch();
// --- Working directory row ---
fWorkDirField = new QLineEdit(QDir::homePath());
fBrowseBtn = new QPushButton("Browse\xe2\x80\xa6");
fBrowseBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// --- API settings box ---
fApiBox = new QGroupBox("API Settings");
QFormLayout* apiLayout = new QFormLayout(fApiBox);
apiLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
fApiUrlField = new QLineEdit("https://api.anthropic.com/v1");
apiLayout->addRow("API URL:", fApiUrlField);
fApiKeyField = new QLineEdit;
fApiKeyField->setEchoMode(QLineEdit::Password);
apiLayout->addRow("API Key:", fApiKeyField);
fSaveApiKeyCheck = new QCheckBox("Remember key");
apiLayout->addRow("", fSaveApiKeyCheck);
// Helper to add a checkbox + hidden field row
auto addOverrideRow = [&](const QString& label, const QString& defaultVal,
QCheckBox*& check, QLineEdit*& field) {
check = new QCheckBox(label);
field = new QLineEdit(defaultVal);
field->setVisible(false);
apiLayout->addRow(check);
apiLayout->addRow(field);
};
addOverrideRow("Override current model", "claude-sonnet-4-6",
fApiCurrentModelCheck, fApiCurrentModelField);
addOverrideRow("Override ANTHROPIC_DEFAULT_OPUS_MODEL", "claude-opus-4-20250514",
fApiOpusModelCheck, fApiOpusModelField);
addOverrideRow("Override ANTHROPIC_DEFAULT_SONNET_MODEL", "claude-sonnet-4-6",
fApiSonnetModelCheck, fApiSonnetModelField);
addOverrideRow("Override ANTHROPIC_DEFAULT_HAIKU_MODEL", "claude-haiku-4-20250514",
fApiHaikuModelCheck, fApiHaikuModelField);
// Custom environment variables
fCustomEnvVarsCheck = new QCheckBox("Custom Environment Variables");
fCustomEnvVarsField = new QTextEdit;
fCustomEnvVarsField->setVisible(false);
fCustomEnvVarsField->setPlaceholderText("CLAUDE_STREAM_IDLE_TIMEOUT_MS=30000\nANTHROPIC_BASE_URL_FALLBACK=https://example.com");
apiLayout->addRow(fCustomEnvVarsCheck);
apiLayout->addRow(fCustomEnvVarsField);
fFixAttributionCheck = new QCheckBox("Fix attribution header (for local LLMs)");
fFixAttributionCheck->setToolTip("Disables CLAUDE_CODE_ATTRIBUTION_HEADER in settings.json.\n"
"Required for local LLMs to avoid 90% slower inference.");
apiLayout->addRow("", fFixAttributionCheck);
// --- Profile management ---
fProfileNameEdit = new QLineEdit;
fProfileNameEdit->setPlaceholderText("Profile name");
fProfileNameEdit->setMaximumWidth(200);
fProfileComboBox = new QComboBox;
fProfileComboBox->setPlaceholderText("Select profile...");
fProfileListWidget = new QListWidget;
fProfileListWidget->setMaximumHeight(150);
fSaveProfileButton = new QPushButton("Save Profile");
fDeleteProfileButton = new QPushButton("Delete Profile");
fDeleteProfileButton->setEnabled(false);
fProfileBox = new QGroupBox("API Profiles");
QVBoxLayout* profileLayout = new QVBoxLayout(fProfileBox);
QHBoxLayout* profileRow = new QHBoxLayout;
profileRow->addWidget(new QLabel("Profile:"));
profileRow->addWidget(fProfileComboBox);
profileRow->addWidget(fProfileNameEdit);
profileRow->addWidget(fSaveProfileButton);
profileLayout->addLayout(profileRow);
profileLayout->addWidget(fProfileListWidget);
profileLayout->addWidget(fDeleteProfileButton);
// --- Launch button ---
fLaunchBtn = new QPushButton("Launch Claude Code");
fLaunchBtn->setDefault(true);
// --- Root layout ---
QVBoxLayout* root = new QVBoxLayout(this);
root->setSpacing(6);
root->setContentsMargins(12, 12, 12, 12);
root->addWidget(new QLabel("Launch mode:"));
// Cloud radio (indented)
QHBoxLayout* cloudRow = new QHBoxLayout;
cloudRow->setContentsMargins(16, 0, 0, 0);
cloudRow->addWidget(fCloudRadio);
cloudRow->addStretch();
root->addLayout(cloudRow);
root->addWidget(fModelBox);
// API radio (indented)
QHBoxLayout* apiRow = new QHBoxLayout;
apiRow->setContentsMargins(16, 0, 0, 0);
apiRow->addWidget(fApiRadio);
apiRow->addStretch();
root->addLayout(apiRow);
// Directory row
QHBoxLayout* dirRow = new QHBoxLayout;
dirRow->addWidget(new QLabel("Directory:"));
dirRow->addWidget(fWorkDirField);
dirRow->addWidget(fBrowseBtn);
root->addLayout(dirRow);
// YOLO checkbox (visible in both modes)
fYoloCheck = new QCheckBox("YOLO mode (skip all permissions)");
fYoloCheck->setToolTip("Adds --dangerously-skip-permissions flag.\n"
"WARNING: Claude will execute commands without asking for approval.");
root->addWidget(fYoloCheck);
root->addWidget(fApiBox);
root->addWidget(fProfileBox);
root->addStretch();
root->addWidget(fLaunchBtn);
// --- Connections ---
connect(fModeGroup, &QButtonGroup::buttonToggled,
this, [this](QAbstractButton*, bool) { updateModeVisibility(); });
connect(fBrowseBtn, &QPushButton::clicked, this, [this]() {
QString dir = QFileDialog::getExistingDirectory(
this, "Select Working Directory",
fWorkDirField->text(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!dir.isEmpty())
fWorkDirField->setText(dir);
});
connect(fLaunchBtn, &QPushButton::clicked, this, [this]() {
// Validate working directory
QString workDir = fWorkDirField->text().trimmed();
if (!workDir.isEmpty() && !QDir(workDir).exists()) {
QMessageBox::warning(this, "Invalid Directory",
"The specified working directory does not exist:\n" + workDir);
return;
}
// Validate API fields if in API mode
if (fApiRadio->isChecked()) {
if (fApiUrlField->text().trimmed().isEmpty()) {
QMessageBox::warning(this, "Missing API URL",
"API URL cannot be empty.");
return;
}
if (fApiKeyField->text().isEmpty()) {
QMessageBox::warning(this, "Missing API Key",
"API Key cannot be empty.");
return;
}
}
saveSettings();
// Apply attribution header fix if in API mode and checkbox is checked
if (fApiRadio->isChecked() && fFixAttributionCheck->isChecked())
applyAttributionHeaderFix();
QString cmd = buildCommand();
if (isatty(STDIN_FILENO)) {
gPendingExec = cmd.toStdString();
} else {
launchInTerminal(cmd);
}
close();
});
auto connectCheck = [this](QCheckBox* cb, QLineEdit* field) {
connect(cb, &QCheckBox::toggled, this, [this, field](bool checked) {
field->setVisible(checked);
resizeToFit();
});
};
connectCheck(fApiCurrentModelCheck, fApiCurrentModelField);
connectCheck(fApiOpusModelCheck, fApiOpusModelField);
connectCheck(fApiSonnetModelCheck, fApiSonnetModelField);
connectCheck(fApiHaikuModelCheck, fApiHaikuModelField);
// Connect custom environment variables checkbox
connect(fCustomEnvVarsCheck, &QCheckBox::toggled, this, [this](bool checked) {
fCustomEnvVarsField->setVisible(checked);
resizeToFit();
});
// Profile management connections
connect(fProfileComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, [this](int index) {
if (index >= 0 && !fProfileComboBox->itemText(index).isEmpty())
loadProfile(fProfileComboBox->itemText(index));
});
connect(fProfileListWidget, &QListWidget::itemSelectionChanged,
this, [this]() {
fDeleteProfileButton->setEnabled(fProfileListWidget->currentItem() != nullptr);
});
connect(fProfileListWidget, &QListWidget::itemDoubleClicked,
this, [this](QListWidgetItem* item) {
if (item) {
int idx = fProfileComboBox->findText(item->text());
if (idx >= 0)
fProfileComboBox->setCurrentIndex(idx);
}
});
connect(fSaveProfileButton, &QPushButton::clicked, this, &LauncherWindow::saveCurrentProfile);
connect(fDeleteProfileButton, &QPushButton::clicked, this, &LauncherWindow::deleteProfile);
// Load profiles on startup
loadProfiles();
// --- Initial state ---
loadSettings();
updateModeVisibility(); // calls resizeToFit()
// Center on screen
QScreen* screen = QGuiApplication::primaryScreen();
if (screen)
move(screen->geometry().center() - rect().center());
}
// ---------------------------------------------------------------------------
// resizeToFit
// ---------------------------------------------------------------------------
void LauncherWindow::resizeToFit()
{
// Unlock size limits so adjustSize() can change both dimensions
setMinimumSize(0, 0);
setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
layout()->activate();
adjustSize();
setFixedSize(size());
}
// ---------------------------------------------------------------------------
// updateModeVisibility
// ---------------------------------------------------------------------------
void LauncherWindow::updateModeVisibility()
{
bool isApi = fApiRadio->isChecked();
fModelBox->setVisible(!isApi);
fApiBox->setVisible(isApi);
fProfileBox->setVisible(isApi);
resizeToFit();
}
// ---------------------------------------------------------------------------
// buildCommand
// ---------------------------------------------------------------------------
QString LauncherWindow::buildCommand() const
{
QString claudeBin = findClaude();
QString workDir = fWorkDirField->text().trimmed();
QString cmd;
if (!workDir.isEmpty())
cmd += "cd " + shellEscape(workDir) + " && ";
if (fCloudRadio->isChecked()) {
cmd += "unset ANTHROPIC_API_KEY && " + claudeBin;
int modelId = fModelGroup->checkedId();
if (modelId == 1) cmd += " --model sonnet";
else if (modelId == 2) cmd += " --model haiku";
else cmd += " --model opus";
} else {
QString apiUrl = fApiUrlField->text();
QString apiKey = fApiKeyField->text();
// Use trap to guarantee credentials are restored on exit
cmd += "trap 'mv \"$HOME/.claude/.credentials.json.bak\" "
"\"$HOME/.claude/.credentials.json\" 2>/dev/null' EXIT; ";
cmd += "mv \"$HOME/.claude/.credentials.json\" "
"\"$HOME/.claude/.credentials.json.bak\" 2>/dev/null; ";
cmd += "ANTHROPIC_BASE_URL=" + shellEscape(apiUrl) +
" ANTHROPIC_API_KEY=" + shellEscape(apiKey);
auto addEnv = [&](QCheckBox* cb, QLineEdit* field, const QString& var) {
if (cb->isChecked() && !field->text().isEmpty())
cmd += " " + var + "=" + shellEscape(field->text());
};
addEnv(fApiOpusModelCheck, fApiOpusModelField, "ANTHROPIC_DEFAULT_OPUS_MODEL");
addEnv(fApiSonnetModelCheck, fApiSonnetModelField, "ANTHROPIC_DEFAULT_SONNET_MODEL");
addEnv(fApiHaikuModelCheck, fApiHaikuModelField, "ANTHROPIC_DEFAULT_HAIKU_MODEL");
// Add custom environment variables
if (fCustomEnvVarsCheck->isChecked()) {
QString content = fCustomEnvVarsField->toPlainText();
QStringList lines = content.split('\n');
for (const QString& line : lines) {
QString trimmed = line.trimmed();
if (!trimmed.isEmpty() && !trimmed.startsWith('#')) {
cmd += " ";
// Parse key=value
int eqPos = trimmed.indexOf('=');
if (eqPos > 0) {
QString key = trimmed.left(eqPos).trimmed();
QString value = trimmed.mid(eqPos + 1).trimmed();
cmd += key + "=" + shellEscape(value);
} else {
// Handle as variable name only (value will be empty)
cmd += trimmed;
}
}
}
}
cmd += " " + claudeBin;
if (fApiCurrentModelCheck->isChecked() && !fApiCurrentModelField->text().isEmpty())
cmd += " --model " + shellEscape(fApiCurrentModelField->text());
}
// Add YOLO flag if checked (applies to both modes)
if (fYoloCheck->isChecked())
cmd += " --dangerously-skip-permissions";
return cmd;
}
// ---------------------------------------------------------------------------
// findClaude
// ---------------------------------------------------------------------------
QString LauncherWindow::findClaude() const
{
const QStringList candidates = {
QDir::homePath() + "/.npm-global/bin/claude",
QDir::homePath() + "/.local/bin/claude",
"/usr/local/bin/claude",
};
for (const QString& path : candidates) {
if (QFile::exists(path))
return path;
}
// Fall back to PATH lookup; the login shell will resolve it
const QString found = QStandardPaths::findExecutable("claude");
return found.isEmpty() ? QStringLiteral("claude") : found;
}
// ---------------------------------------------------------------------------
// findTerminal
// ---------------------------------------------------------------------------
QString LauncherWindow::findTerminal() const
{
// 1. Honour $TERMINAL if set
const QString envTerm = qEnvironmentVariable("TERMINAL");
if (!envTerm.isEmpty()) {
const QString path = QStandardPaths::findExecutable(envTerm);
if (!path.isEmpty())
return path;
}
// 2. Common terminal emulators in order of preference
const QStringList candidates = {
"xterm", "alacritty", "kitty",
"gnome-terminal", "konsole",
"xfce4-terminal", "lxterminal", "x-terminal-emulator",
};
for (const QString& name : candidates) {
const QString path = QStandardPaths::findExecutable(name);
if (!path.isEmpty())
return path;
}
return QString();
}
// ---------------------------------------------------------------------------
// launchInTerminal
// ---------------------------------------------------------------------------
void LauncherWindow::launchInTerminal(const QString& cmd)
{
const QString termPath = findTerminal();
if (termPath.isEmpty()) {
QMessageBox::critical(this, "No Terminal Found",
"Could not find a terminal emulator.\n"
"Please install xterm, alacritty, konsole, or gnome-terminal,\n"
"or set the $TERMINAL environment variable.");
return;
}
const QString termName = QFileInfo(termPath).fileName();
// Use a login shell so the user's PATH and env are fully initialised
QStringList args;
if (termName == "gnome-terminal")
args = {"--", "bash", "-l", "-c", cmd};
else if (termName == "kitty")
args = {"bash", "-l", "-c", cmd};
else
args = {"-e", "bash", "-l", "-c", cmd};
QProcess::startDetached(termPath, args);
}
// ---------------------------------------------------------------------------
// applyAttributionHeaderFix
// ---------------------------------------------------------------------------
void LauncherWindow::applyAttributionHeaderFix()
{
QString settingsPath = QDir::homePath() + "/.claude/settings.json";
QJsonObject root;
// Read existing settings if file exists
QFile file(settingsPath);
if (file.exists()) {
if (file.open(QIODevice::ReadOnly)) {
QByteArray data = file.readAll();
file.close();
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
if (parseError.error == QJsonParseError::NoError && doc.isObject())
root = doc.object();
}
}
// Ensure "env" object exists and add the fix
QJsonObject env = root.value("env").toObject();
env.insert("CLAUDE_CODE_ATTRIBUTION_HEADER", "0");
root.insert("env", env);
// Write back
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::warning(this, "Settings Error",
"Could not write to ~/.claude/settings.json");
return;
}
file.write(QJsonDocument(root).toJson(QJsonDocument::Indented));
file.close();
}
// ---------------------------------------------------------------------------
// loadSettings / saveSettings
// ---------------------------------------------------------------------------
void LauncherWindow::loadSettings()
{
QSettings s;
const int mode = s.value("mode", 0).toInt();
if (mode == 1) fApiRadio->setChecked(true);
else fCloudRadio->setChecked(true);
const int cloudModel = s.value("cloudModel", 0).toInt();
if (cloudModel == 1) fCloudSonnetRadio->setChecked(true);
else if (cloudModel == 2) fCloudHaikuRadio->setChecked(true);
else fCloudOpusRadio->setChecked(true);
fWorkDirField->setText(s.value("workDir", QDir::homePath()).toString());
// Load profile settings if in API mode
if (fApiRadio->isChecked()) {
QString profileName = s.value("activeProfile", "").toString();
if (!profileName.isEmpty()) {
loadProfile(profileName);
} else {
// Load individual settings
fApiUrlField->setText( s.value("apiUrl", "https://api.anthropic.com/v1").toString());
fApiKeyField->setText(s.value("apiKey", "").toString());
const bool saveKey = s.value("saveApiKey", false).toBool();
fSaveApiKeyCheck->setChecked(saveKey);
if (saveKey)
fApiKeyField->setText(s.value("apiKey", "").toString());
auto loadCheck = [&](const QString& checkKey, QCheckBox* cb,
const QString& fieldKey, const QString& defaultVal, QLineEdit* field) {
const bool checked = s.value(checkKey, false).toBool();
cb->setChecked(checked);
field->setVisible(checked);
field->setText(s.value(fieldKey, defaultVal).toString());
};
loadCheck("apiCurrentModelCheck", fApiCurrentModelCheck,
"apiCurrentModel", "claude-sonnet-4-6", fApiCurrentModelField);
loadCheck("apiOpusModelCheck", fApiOpusModelCheck,
"apiOpusModel", "claude-opus-4-20250514", fApiOpusModelField);
loadCheck("apiSonnetModelCheck", fApiSonnetModelCheck,
"apiSonnetModel", "claude-sonnet-4-6", fApiSonnetModelField);
loadCheck("apiHaikuModelCheck", fApiHaikuModelCheck,
"apiHaikuModel", "claude-haiku-4-20250514", fApiHaikuModelField);
fCustomEnvVarsCheck->setChecked(s.value("customEnvVarsCheck", false).toBool());
fCustomEnvVarsField->setVisible(fCustomEnvVarsCheck->isChecked());
fCustomEnvVarsField->setPlainText(s.value("customEnvVarsContent", "").toString());
fFixAttributionCheck->setChecked(s.value("fixAttributionHeader", false).toBool());
fYoloCheck->setChecked(s.value("yoloMode", false).toBool());
}
} else {
// Load individual settings for Cloud mode
fApiUrlField->setText( s.value("apiUrl", "https://api.anthropic.com/v1").toString());
fApiKeyField->setText(s.value("apiKey", "").toString());
const bool saveKey = s.value("saveApiKey", false).toBool();
fSaveApiKeyCheck->setChecked(saveKey);
if (saveKey)
fApiKeyField->setText(s.value("apiKey", "").toString());
auto loadCheck = [&](const QString& checkKey, QCheckBox* cb,
const QString& fieldKey, const QString& defaultVal, QLineEdit* field) {
const bool checked = s.value(checkKey, false).toBool();
cb->setChecked(checked);
field->setVisible(checked);
field->setText(s.value(fieldKey, defaultVal).toString());
};
loadCheck("apiCurrentModelCheck", fApiCurrentModelCheck,
"apiCurrentModel", "claude-sonnet-4-6", fApiCurrentModelField);
loadCheck("apiOpusModelCheck", fApiOpusModelCheck,
"apiOpusModel", "claude-opus-4-20250514", fApiOpusModelField);
loadCheck("apiSonnetModelCheck", fApiSonnetModelCheck,
"apiSonnetModel", "claude-sonnet-4-6", fApiSonnetModelField);
loadCheck("apiHaikuModelCheck", fApiHaikuModelCheck,
"apiHaikuModel", "claude-haiku-4-20250514", fApiHaikuModelField);
fCustomEnvVarsCheck->setChecked(s.value("customEnvVarsCheck", false).toBool());
fCustomEnvVarsField->setVisible(fCustomEnvVarsCheck->isChecked());
fCustomEnvVarsField->setPlainText(s.value("customEnvVarsContent", "").toString());
fFixAttributionCheck->setChecked(s.value("fixAttributionHeader", false).toBool());
fYoloCheck->setChecked(s.value("yoloMode", false).toBool());
}
}
void LauncherWindow::saveSettings()
{
QSettings s;
s.setValue("mode", fApiRadio->isChecked() ? 1 : 0);
const int cloudModel = fModelGroup->checkedId();
s.setValue("cloudModel", cloudModel >= 0 ? cloudModel : 0);
s.setValue("workDir", fWorkDirField->text());
// Save active profile if in API mode
if (fApiRadio->isChecked()) {
QString profileName = fProfileComboBox->currentText();
if (!profileName.isEmpty()) {
s.setValue("activeProfile", profileName);
// Also save current settings to the active profile
s.setValue("apiProfile_" + profileName + "_url", fApiUrlField->text());
s.setValue("apiProfile_" + profileName + "_key", fApiKeyField->text());
s.setValue("apiProfile_" + profileName + "_saveKey", fSaveApiKeyCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_currentModelCheck", fApiCurrentModelCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_currentModel", fApiCurrentModelField->text());
s.setValue("apiProfile_" + profileName + "_opusModelCheck", fApiOpusModelCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_opusModel", fApiOpusModelField->text());
s.setValue("apiProfile_" + profileName + "_sonnetModelCheck", fApiSonnetModelCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_sonnetModel", fApiSonnetModelField->text());
s.setValue("apiProfile_" + profileName + "_haikuModelCheck", fApiHaikuModelCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_haikuModel", fApiHaikuModelField->text());
s.setValue("apiProfile_" + profileName + "_fixAttribution", fFixAttributionCheck->isChecked());
}
}
s.setValue("apiUrl", fApiUrlField->text());
// Only save API key if checkbox is checked
s.setValue("saveApiKey", fSaveApiKeyCheck->isChecked());
if (fSaveApiKeyCheck->isChecked())
s.setValue("apiKey", fApiKeyField->text());
s.setValue("apiCurrentModelCheck", fApiCurrentModelCheck->isChecked());
s.setValue("apiCurrentModel", fApiCurrentModelField->text());
s.setValue("apiOpusModelCheck", fApiOpusModelCheck->isChecked());
s.setValue("apiOpusModel", fApiOpusModelField->text());
s.setValue("apiSonnetModelCheck", fApiSonnetModelCheck->isChecked());
s.setValue("apiSonnetModel", fApiSonnetModelField->text());
s.setValue("apiHaikuModelCheck", fApiHaikuModelCheck->isChecked());
s.setValue("apiHaikuModel", fApiHaikuModelField->text());
s.setValue("customEnvVarsCheck", fCustomEnvVarsCheck->isChecked());
s.setValue("customEnvVarsContent", fCustomEnvVarsField->toPlainText());
s.setValue("fixAttributionHeader", fFixAttributionCheck->isChecked());
s.setValue("yoloMode", fYoloCheck->isChecked());
}
// ---------------------------------------------------------------------------
// Profile Management
// ---------------------------------------------------------------------------
void LauncherWindow::loadProfiles()
{
QSettings s;
QStringList profiles = s.value("apiProfiles").toStringList();
updateProfileComboBox();
updateProfileListWidget();
}
void LauncherWindow::updateProfileComboBox()
{
QSettings s;
QStringList profiles = s.value("apiProfiles").toStringList();
fProfileComboBox->clear();
fProfileComboBox->addItems(profiles);
// Select first profile if available
if (!profiles.isEmpty())
fProfileComboBox->setCurrentIndex(0);
}
void LauncherWindow::updateProfileListWidget()
{
QSettings s;
QStringList profiles = s.value("apiProfiles").toStringList();
fProfileListWidget->clear();
for (const QString& profile : profiles) {
fProfileListWidget->addItem(profile);
}
}
void LauncherWindow::saveCurrentProfile()
{
QSettings s;
QStringList profiles = s.value("apiProfiles").toStringList();
// Check name field first for new profiles
QString profileName = fProfileNameEdit->text().trimmed();
bool isUpdate = false;
if (!profileName.isEmpty()) {
// User entered a name - check for duplicate
if (profiles.contains(profileName)) {
QMessageBox::warning(this, "Duplicate Profile",
"A profile with this name already exists.");
return;
}
} else {
// No name entered - use selected profile from combo box (update existing)
profileName = fProfileComboBox->currentText();
if (profileName.isEmpty() || !profiles.contains(profileName)) {
QMessageBox::warning(this, "Invalid Profile Name",
"Please enter a new profile name or select an existing profile to update.");
return;
}
isUpdate = true;
}
// Save profile settings
s.setValue("apiProfile_" + profileName + "_url", fApiUrlField->text());
s.setValue("apiProfile_" + profileName + "_key", fApiKeyField->text());
s.setValue("apiProfile_" + profileName + "_saveKey", fSaveApiKeyCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_currentModelCheck", fApiCurrentModelCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_currentModel", fApiCurrentModelField->text());
s.setValue("apiProfile_" + profileName + "_opusModelCheck", fApiOpusModelCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_opusModel", fApiOpusModelField->text());
s.setValue("apiProfile_" + profileName + "_sonnetModelCheck", fApiSonnetModelCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_sonnetModel", fApiSonnetModelField->text());
s.setValue("apiProfile_" + profileName + "_haikuModelCheck", fApiHaikuModelCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_haikuModel", fApiHaikuModelField->text());
s.setValue("apiProfile_" + profileName + "_customEnvVarsCheck", fCustomEnvVarsCheck->isChecked());
s.setValue("apiProfile_" + profileName + "_customEnvVarsContent", fCustomEnvVarsField->toPlainText());
s.setValue("apiProfile_" + profileName + "_fixAttribution", fFixAttributionCheck->isChecked());
// Add to profiles list only if new
if (!isUpdate) {
profiles.append(profileName);
s.setValue("apiProfiles", profiles);
}
// Update UI
updateProfileComboBox();
updateProfileListWidget();
fProfileNameEdit->clear();
// Select the saved/updated profile in combo box
int idx = fProfileComboBox->findText(profileName);
if (idx >= 0)
fProfileComboBox->setCurrentIndex(idx);
QMessageBox::information(this, "Profile Saved",
isUpdate ? "Profile '" + profileName + "' has been updated."
: "Profile '" + profileName + "' has been saved.");
}
void LauncherWindow::deleteProfile()
{
QListWidgetItem* item = fProfileListWidget->currentItem();
if (!item) {
QMessageBox::warning(this, "No Selection",
"Please select a profile to delete.");
return;
}
QString profileName = item->text();
// Remove from profiles list
QSettings s;
QStringList profiles = s.value("apiProfiles").toStringList();
profiles.removeAll(profileName);
s.setValue("apiProfiles", profiles);
// Remove profile settings
s.remove("apiProfile_" + profileName + "_url");
s.remove("apiProfile_" + profileName + "_key");
s.remove("apiProfile_" + profileName + "_saveKey");
s.remove("apiProfile_" + profileName + "_currentModelCheck");
s.remove("apiProfile_" + profileName + "_currentModel");
s.remove("apiProfile_" + profileName + "_opusModelCheck");
s.remove("apiProfile_" + profileName + "_opusModel");
s.remove("apiProfile_" + profileName + "_sonnetModelCheck");
s.remove("apiProfile_" + profileName + "_sonnetModel");
s.remove("apiProfile_" + profileName + "_haikuModelCheck");
s.remove("apiProfile_" + profileName + "_haikuModel");
s.remove("apiProfile_" + profileName + "_customEnvVarsCheck");
s.remove("apiProfile_" + profileName + "_customEnvVarsContent");
s.remove("apiProfile_" + profileName + "_fixAttribution");
// Update UI
updateProfileComboBox();
updateProfileListWidget();
QMessageBox::information(this, "Profile Deleted",
"Profile '" + profileName + "' has been deleted.");
}
void LauncherWindow::loadProfile(const QString& name)
{
QSettings s;
// Load profile settings
fApiUrlField->setText(s.value("apiProfile_" + name + "_url", "https://api.anthropic.com/v1").toString());
fApiKeyField->setText(s.value("apiProfile_" + name + "_key", "").toString());
fSaveApiKeyCheck->setChecked(s.value("apiProfile_" + name + "_saveKey", false).toBool());
fApiCurrentModelCheck->setChecked(s.value("apiProfile_" + name + "_currentModelCheck", false).toBool());
fApiCurrentModelField->setText(s.value("apiProfile_" + name + "_currentModel", "claude-sonnet-4-6").toString());
fApiOpusModelCheck->setChecked(s.value("apiProfile_" + name + "_opusModelCheck", false).toBool());
fApiOpusModelField->setText(s.value("apiProfile_" + name + "_opusModel", "claude-opus-4-20250514").toString());
fApiSonnetModelCheck->setChecked(s.value("apiProfile_" + name + "_sonnetModelCheck", false).toBool());
fApiSonnetModelField->setText(s.value("apiProfile_" + name + "_sonnetModel", "claude-sonnet-4-6").toString());
fApiHaikuModelCheck->setChecked(s.value("apiProfile_" + name + "_haikuModelCheck", false).toBool());
fApiHaikuModelField->setText(s.value("apiProfile_" + name + "_haikuModel", "claude-haiku-4-20250514").toString());
fCustomEnvVarsCheck->setChecked(s.value("apiProfile_" + name + "_customEnvVarsCheck", false).toBool());
fCustomEnvVarsField->setPlainText(s.value("apiProfile_" + name + "_customEnvVarsContent", "").toString());
fFixAttributionCheck->setChecked(s.value("apiProfile_" + name + "_fixAttribution", false).toBool());
// Update visibility
fApiCurrentModelField->setVisible(fApiCurrentModelCheck->isChecked());
fApiOpusModelField->setVisible(fApiOpusModelCheck->isChecked());
fApiSonnetModelField->setVisible(fApiSonnetModelCheck->isChecked());
fApiHaikuModelField->setVisible(fApiHaikuModelCheck->isChecked());
fCustomEnvVarsField->setVisible(fCustomEnvVarsCheck->isChecked());
resizeToFit();
}