-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
136 lines (125 loc) · 5.24 KB
/
mainwindow.cpp
File metadata and controls
136 lines (125 loc) · 5.24 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSystemTrayIcon>
#include "quickmemo.h"
#include "IniConfig.h"
#include <QWebEngineProfile>
#include <QWebEngineCookieStore>
#include <QWebEngineSettings>
#include <QInputDialog>
#include <QShortcut>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//creat trayIcon
QApplication::setQuitOnLastWindowClosed(false);
trayIcon=new QSystemTrayIcon(this);
auto icon = QIcon(":/memos.png");//前面不应该加qrc。。。
trayIcon->setIcon(icon);
setWindowIcon(icon);
trayIcon->setToolTip("QMemos");
trayIcon->showMessage("title","text",QSystemTrayIcon::Information,3000);
QAction *quit=new QAction(QStringLiteral("quit"));
connect(quit,&QAction::triggered,this,[&](){//修正,避免一个警告
m_webview.reset();//避免提示Release of profile requested but WebEnginePage still not deleted. Expect troubles !
QCoreApplication::quit();
});
//添加手动设置信息的方法
QAction *setUrl=new QAction(QStringLiteral("set Url"));//本应在第一次启动的时候添加
connect(setUrl,&QAction::triggered,this,[this](){
QString url;
IniConfig a;
while(url.isEmpty()){
url=QInputDialog::getText(this,tr("设定服务器地址"),tr("请输入memos服务器地址:"),
QLineEdit::Normal,a.value("memos/url").toString());
}
a.setValue("memos/url",url);
});
QAction *setUsername=new QAction(QStringLiteral("set Username"));//只能在此处设置
connect(setUsername,&QAction::triggered,this,[this](){
QString username;
IniConfig a;
while(username.isEmpty()){
username=QInputDialog::getText(this,tr("设定用户名"),tr("请输入用户名:"),
QLineEdit::Normal,a.value("memos/username").toString());
}
a.setValue("memos/username",username);
});
QAction *setToken=new QAction(QStringLiteral("set Access-Token"));//本应在登录的时候自动读取
connect(setToken,&QAction::triggered,this,[this](){
QString token;
IniConfig a;
while(token.isEmpty()){
token=QInputDialog::getText(this,tr("设定access_token"),tr("请输入access_token:"),
QLineEdit::Normal,a.value("memos/access_token").toString());
}
a.setValue("memos/access_token",token);
});
trayMenu=new QMenu;
trayMenu->addAction(setUrl);
//trayMenu->addAction(setUsername);
trayMenu->addAction(setToken);
trayIcon->setContextMenu(trayMenu);
trayMenu->addAction(quit);
setWindowTitle(tr("QMemos"));
// connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &MainWindow::messageClicked);
// connect(trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::iconActivated);
connect(trayIcon,&QSystemTrayIcon::activated,this,&MainWindow::whenTrayIconActivated);
trayIcon->show();
// F5刷新
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_F5), this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::reload);
//read config
IniConfig a;
hotkey = new QHotkey(QKeySequence(a.value("hotkey").toString()),true,qApp);
qDebug()<< "set hotkey"<< a.value("hotkey").toString();
connect(hotkey,&QHotkey::activated,this,&MainWindow::whenHotkeyActivated);
QString url=a.value("memos/url").toString();
qDebug()<<url;
while(url.isEmpty()){
url=QInputDialog::getText(this,tr("设定服务器地址"),tr("请输入memos服务器地址:"));
}
a.setValue("memos/url",url);
m_profile.reset(new QWebEngineProfile("QMemos"));
// m_profile->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
// m_profile->settings()->setAttribute(QWebEngineSettings::DnsPrefetchEnabled, true);
// m_profile->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
// m_profile->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, false);
qDebug()<<m_profile->isOffTheRecord();
//m_profile->cookieStore()->deleteAllCookies();
connect(m_profile->cookieStore(),&QWebEngineCookieStore::cookieAdded, this, &MainWindow::handleCookieAdded);
m_webview.reset(new QWebEngineView(m_profile.get()));
this->resize(800, 600);
this->setCentralWidget(m_webview.get());//原来这么简单
m_webview->setUrl(QUrl(url));
// ui->webEngineView->load(QUrl("http://111.229.25.29:5231/"));
//auto cookiestore=ui->webEngineView->page()->profile()->cookieStore();
}
#include <QNetworkCookie>
#include <QWebEngineCookieStore>
MainWindow::~MainWindow()
{
IniConfig a;
delete ui;
}
void MainWindow::whenHotkeyActivated(){
QuickMemo w;
w.exec();
reload();
}
void MainWindow::whenTrayIconActivated(QSystemTrayIcon::ActivationReason reason){
switch (reason) {
case 2:;
case 3:this->show();this->activateWindow();break;
default:
break;
}
}
void MainWindow::handleCookieAdded(const QNetworkCookie &cookie){
if(cookie.name()=="memos.access-token"){
auto token=cookie.value();
IniConfig a;
a.setValue("memos/access_token",token);
}
}