Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F134406
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
13 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/AvatarLoader.cpp b/src/AvatarLoader.cpp
index f12e0c0..d7deba6 100644
--- a/src/AvatarLoader.cpp
+++ b/src/AvatarLoader.cpp
@@ -1,189 +1,187 @@
#include "AvatarLoader.h"
#include "BasicMainWindow.h"
#include "MemoryReader.h"
#include "webclient.h"
#include <QCryptographicHash>
#include <QDebug>
namespace {
const int MAX_CACHE_COUNT = 1000;
const int ICON_SIZE = 64;
}
using WebClientPtr = std::shared_ptr<WebClient>;
struct AvatarLoader::Private {
QMutex data_mutex;
QMutex thread_mutex;
QWaitCondition condition;
std::map<std::string, std::string> avatar_url_cache;
std::deque<RequestItem> requested;
std::deque<RequestItem> completed;
std::set<std::string> notfound;
BasicMainWindow *mainwindow = nullptr;
- WebContext *webcx = nullptr;
WebClientPtr web;
};
AvatarLoader::AvatarLoader()
: m(new Private)
{
}
AvatarLoader::~AvatarLoader()
{
delete m;
}
void AvatarLoader::start(BasicMainWindow *mainwindow)
{
m->mainwindow = mainwindow;
- m->webcx = m->mainwindow->webContext();
QThread::start();
}
void AvatarLoader::run()
{
- m->web = std::make_shared<WebClient>(m->webcx);
+ m->web = std::make_shared<WebClient>(m->mainwindow->webContext());
while (1) {
std::deque<RequestItem> requests;
if (isInterruptionRequested()) return;
m->thread_mutex.lock();
{
QMutexLocker lock(&m->data_mutex);
requests = std::move(m->requested);
}
if (requests.empty()) {
m->condition.wait(&m->thread_mutex);
{
QMutexLocker lock(&m->data_mutex);
requests = std::move(m->requested);
}
}
m->thread_mutex.unlock();
if (isInterruptionRequested()) return;
for (RequestItem &item : requests) {
if (isInterruptionRequested()) return;
if (item.email.empty()) continue;
if (strchr(item.email.c_str(), '@')) {
QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(item.email.c_str(), item.email.size());
QByteArray ba = hash.result();
char tmp[100];
for (int i = 0; i < ba.size(); i++) {
sprintf(tmp + i * 2, "%02x", ba.data()[i] & 0xff);
}
QString id = tmp;
QString url = "https://www.gravatar.com/avatar/%1?s=%2";
url = url.arg(id).arg(ICON_SIZE);
if (m->web->get(WebClient::URL(url.toStdString())) == 200) {
if (!m->web->response().content.empty()) {
MemoryReader reader(m->web->response().content.data(), m->web->response().content.size());
reader.open(MemoryReader::ReadOnly);
QImage image;
image.load(&reader, nullptr);
int w = image.width();
int h = image.height();
if (w > 0 && h > 0) {
item.icon = QIcon(QPixmap::fromImage(image));
{
QMutexLocker lock(&m->data_mutex);
while (m->completed.size() >= MAX_CACHE_COUNT) {
m->completed.pop_back();
}
m->completed.push_front(item);
}
emit updated();
continue;
}
}
} else {
m->mainwindow->emitWriteLog(QString("Failed to fetch the avatar.\n").toUtf8());
QString msg = QString::fromStdString(m->web->error().message() + '\n');
m->mainwindow->emitWriteLog(msg.toUtf8());
}
}
{ // not found
QMutexLocker lock(&m->data_mutex);
m->notfound.insert(m->notfound.end(), item.email);
}
}
}
}
namespace {
bool isValidGitHubName(std::string const &name)
{
size_t n = name.size();
if (n < 1 || n > 39) return false;
char const *begin = name.c_str();
char const *end = begin + name.size();
char const *ptr = begin;
while (ptr < end) {
int c = (unsigned char)*ptr;
ptr++;
if (isalnum(c)) {
// ok
} else if (c == '-') {
if (ptr == begin) return false;
if (ptr < end && *ptr == '-') return false;
}
}
return true;
}
}
QIcon AvatarLoader::fetch(std::string const &email, bool request) const
{
RequestItem item;
item.email = email;
if (isValidGitHubName(item.email)) {
QMutexLocker lock(&m->data_mutex);
auto it = m->notfound.find(item.email);
if (it == m->notfound.end()) {
for (size_t i = 0; i < m->completed.size(); i++) {
if (item.email == m->completed[i].email) {
item = m->completed[i];
m->completed.erase(m->completed.begin() + i);
m->completed.insert(m->completed.begin(), item);
return item.icon;
}
}
if (request) {
bool waiting = false;
for (RequestItem const &r : m->requested) {
if (item.email == r.email) {
waiting = true;
break;
}
}
if (!waiting) {
m->requested.push_back(item);
m->condition.wakeOne();
}
}
}
}
return QIcon();
}
void AvatarLoader::stop()
{
requestInterruption();
if (m->web) m->web->close();
m->condition.wakeAll();
wait();
}
diff --git a/src/GitHubAPI.cpp b/src/GitHubAPI.cpp
index 6cbb41b..57a3285 100644
--- a/src/GitHubAPI.cpp
+++ b/src/GitHubAPI.cpp
@@ -1,111 +1,111 @@
#include "BasicMainWindow.h"
#include "GitHubAPI.h"
#include "webclient.h"
#include "common/misc.h"
#include "charvec.h"
#include "urlencode.h"
#include "MemoryReader.h"
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <memory>
using WebClientPtr = GitHubAPI::WebClientPtr;
struct GitHubRequestThread::Private {
BasicMainWindow *mainwindow = nullptr;
WebClientPtr web;
};
GitHubRequestThread::GitHubRequestThread()
: m(new Private)
{
}
GitHubRequestThread::~GitHubRequestThread()
{
delete m;
}
-void GitHubRequestThread::start(WebContext *webcx, BasicMainWindow *mainwindow)
+void GitHubRequestThread::start(BasicMainWindow *mainwindow)
{
m->mainwindow = mainwindow;
m->web = std::make_shared<WebClient>(m->mainwindow->webContext());
QThread::start();
}
void GitHubRequestThread::run()
{
ok = false;
if (web()->get(WebClient::URL(url)) == 200) {
WebClient::Response const &r = web()->response();
if (!r.content.empty()) {
text = to_stdstr(r.content);
ok = true;
if (callback) {
ok = callback(text);
}
}
} else {
std::string msg = web()->error().message();
if (!msg.empty()) {
m->mainwindow->emitWriteLog(QString::fromStdString("Failed to access the site: " + url + '\n').toUtf8());
QString s = QString::fromStdString(msg + '\n');
m->mainwindow->emitWriteLog(s.toUtf8());
}
}
}
GitHubAPI::WebClientPtr GitHubRequestThread::web()
{
return m->web;
}
QList<GitHubAPI::SearchResultItem> GitHubAPI::searchRepository(std::string const &q)
{
QList<GitHubAPI::SearchResultItem> items;
GitHubRequestThread th;
{
OverrideWaitCursor;
th.url = "https://api.github.com/search/repositories?q=" + q;
- th.start(webcx, mainwindow_);
+ th.start(mainwindow_);
while (!th.wait(1)) {
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
}
if (th.ok) {
QByteArray ba(th.text.c_str(), th.text.size());
QJsonDocument doc = QJsonDocument::fromJson(ba);
QJsonArray a1 = doc.object().value("items").toArray();
for (QJsonValue const &v1 : a1) {
QJsonObject o1 = v1.toObject();
SearchResultItem item;
auto String = [&](QString const &key){
return o1.value(key).toString().toStdString();
};
item.full_name = String("full_name");
if (!item.full_name.empty()) {
item.description = String("description");
item.html_url = String("html_url");
item.ssh_url = String("ssh_url");
item.clone_url = String("clone_url");
item.score = o1.value("score").toDouble();
items.push_back(item);
}
}
}
std::sort(items.begin(), items.end(), [](SearchResultItem const &l, SearchResultItem const &r){
return l.score > r.score; // 降順
});
return items;
}
diff --git a/src/GitHubAPI.h b/src/GitHubAPI.h
index c7b572e..42583d6 100644
--- a/src/GitHubAPI.h
+++ b/src/GitHubAPI.h
@@ -1,65 +1,63 @@
#ifndef GITHUBAPI_H
#define GITHUBAPI_H
#include <QImage>
#include <QThread>
#include <string>
#include <functional>
#include <memory>
class BasicMainWindow;
class WebContext;
class WebClient;
class GitHubAPI {
public:
using WebClientPtr = std::shared_ptr<WebClient>;
struct User {
std::string login;
std::string avatar_url;
std::string name;
std::string email;
};
struct SearchResultItem {
std::string full_name;
std::string description;
std::string ssh_url;
std::string clone_url;
std::string html_url;
double score = 0;
};
BasicMainWindow *mainwindow_;
- WebContext *webcx;
- GitHubAPI(WebContext *webcx, BasicMainWindow *mainwindow)
- : webcx(webcx)
- , mainwindow_(mainwindow)
+ GitHubAPI(BasicMainWindow *mainwindow)
+ : mainwindow_(mainwindow)
{
}
QList<GitHubAPI::SearchResultItem> searchRepository(std::string const &q);
};
class GitHubRequestThread : public QThread {
private:
struct Private;
Private *m;
protected:
void run() override;
public:
GitHubAPI::WebClientPtr web();
public:
GitHubRequestThread();
~GitHubRequestThread() override;
std::string url;
bool ok = false;
std::string text;
std::function<bool(std::string const &text)> callback;
- void start(WebContext *webcx, BasicMainWindow *mainwindow);
+ void start(BasicMainWindow *mainwindow);
};
#endif // GITHUBAPI_H
diff --git a/src/SearchFromGitHubDialog.cpp b/src/SearchFromGitHubDialog.cpp
index a5f74cf..3fe4b90 100644
--- a/src/SearchFromGitHubDialog.cpp
+++ b/src/SearchFromGitHubDialog.cpp
@@ -1,169 +1,169 @@
#include "SearchFromGitHubDialog.h"
#include "ui_SearchFromGitHubDialog.h"
#include "common/misc.h"
#include "urlencode.h"
#include "BasicMainWindow.h"
#include <QDebug>
#include <QThread>
#include <QDesktopServices>
#include <QUrl>
#include <functional>
using SearchResultItem = GitHubAPI::SearchResultItem;
static QString toQString(std::string const &s)
{
return QString::fromUtf8(s.c_str(), s.size());
}
SearchFromGitHubDialog::SearchFromGitHubDialog(QWidget *parent, BasicMainWindow *mw)
: QDialog(parent)
, ui(new Ui::SearchFromGitHubDialog)
, mainwindow(mw)
{
ui->setupUi(this);
Qt::WindowFlags flags = windowFlags();
flags &= ~Qt::WindowContextHelpButtonHint;
setWindowFlags(flags);
ui->tableWidget->setItemDelegate(&item_delegate);
connect(ui->label_hyperlink, &HyperLinkLabel::clicked, this, &SearchFromGitHubDialog::onHyperlinkClicked);
ui->pushButton_ok->setEnabled(false);
}
SearchFromGitHubDialog::~SearchFromGitHubDialog()
{
delete ui;
}
QString SearchFromGitHubDialog::url() const
{
return url_;
}
void SearchFromGitHubDialog::on_pushButton_search_clicked()
{
std::string q = ui->lineEdit_keywords->text().trimmed().toStdString();
q = url_encode(q);
if (q.empty()) return;
- GitHubAPI github(mainwindow->webContext(), mainwindow);
+ GitHubAPI github(mainwindow);
items = github.searchRepository(q);
QStringList cols = {
tr("Name"),
tr("Owner"),
tr("Score"),
tr("Description"),
};
ui->tableWidget->setColumnCount(cols.size());
ui->tableWidget->setRowCount(items.size());
for (int col = 0; col < cols.size(); col++) {
QTableWidgetItem *p = new QTableWidgetItem();
p->setText(cols[col]);
ui->tableWidget->setHorizontalHeaderItem(col, p);
}
for (int row = 0; row < items.size(); row++) {
SearchResultItem const &item = items[row];
QTableWidgetItem *p;
QString name = QString::fromStdString(item.full_name);
QString owner;
int i = name.indexOf('/');
if (i > 0) {
owner = name.mid(0, i);
name = name.mid(i + 1);
}
int col = 0;
auto AddItem = [&](std::function<void(QTableWidgetItem *)> callback){
p = new QTableWidgetItem();
callback(p);
ui->tableWidget->setItem(row, col, p);
col++;
};
AddItem([&](QTableWidgetItem *p){
p->setData(Qt::UserRole, (int)row);
p->setText(name);
});
AddItem([&](QTableWidgetItem *p){
p->setText(owner);
});
AddItem([&](QTableWidgetItem *p){
char tmp[100];
sprintf(tmp, "%.2f", item.score);
p->setText(tmp);
p->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
});
AddItem([&](QTableWidgetItem *p){
p->setText(toQString(item.description));
});
ui->tableWidget->setRowHeight(row, 24);
}
ui->tableWidget->resizeColumnsToContents();
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
}
void SearchFromGitHubDialog::updateUI()
{
auto Check = [&](){
if (ui->radioButton_ssh->isChecked()) {
url_ = ui->lineEdit_ssh->text();
if (!url_.isEmpty()) {
return true;
}
}
if (ui->radioButton_http->isChecked()) {
url_ = ui->lineEdit_http->text();
if (!url_.isEmpty()) {
return true;
}
}
url_ = QString();
return false;
};
ui->pushButton_ok->setEnabled(Check());
}
void SearchFromGitHubDialog::on_tableWidget_currentItemChanged(QTableWidgetItem * /*current*/, QTableWidgetItem * /*previous*/)
{
int row = ui->tableWidget->currentRow();
QTableWidgetItem *p = ui->tableWidget->item(row, 0);
if (p) {
int i = p->data(Qt::UserRole).toInt();
if (i < items.size()) {
SearchResultItem const &item = items[i];
ui->lineEdit_ssh->setText(toQString(item.ssh_url));
ui->lineEdit_http->setText(toQString(item.clone_url));
ui->label_hyperlink->setText(toQString(item.html_url));
}
}
updateUI();
}
void SearchFromGitHubDialog::on_radioButton_ssh_clicked()
{
updateUI();
}
void SearchFromGitHubDialog::on_radioButton_http_clicked()
{
updateUI();
}
void SearchFromGitHubDialog::onHyperlinkClicked()
{
QString url = ui->label_hyperlink->text();
QDesktopServices::openUrl(QUrl(url));
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, Jun 17, 9:30 PM (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
71133
Default Alt Text
(13 KB)
Attached To
Mode
R77 Guitar
Attached
Detach File
Event Timeline