Skip to content

Commit

Permalink
Merge pull request #257 from vktr/f/qr-code-generator
Browse files Browse the repository at this point in the history
Add QR code generator to support apps.
  • Loading branch information
vktr committed May 12, 2016
2 parents cfe7c51 + 832116c commit 65457b2
Show file tree
Hide file tree
Showing 24 changed files with 1,812 additions and 4 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ set(PICOTORRENT_SOURCES
src/client/net/http_client
src/client/net/http_response
src/client/net/uri

# QR components
src/client/qr/bit_buffer
src/client/qr/polynomial
src/client/qr/qr_8bit_byte
src/client/qr/qr_code
src/client/qr/qr_data
src/client/qr/qr_math
src/client/qr/qr_util
src/client/qr/rs_block

src/client/ui/controls/control_base
src/client/ui/controls/list_view
src/client/ui/controls/menu
Expand All @@ -92,6 +103,7 @@ set(PICOTORRENT_SOURCES
src/client/ui/dialogs/add_torrent_dialog
src/client/ui/dialogs/add_tracker_dialog
src/client/ui/dialogs/magnet_link_dialog
src/client/ui/dialogs/remote_qr_dialog
src/client/ui/property_sheets/property_sheet_page
src/client/ui/property_sheets/details/files_page
src/client/ui/property_sheets/details/options_page
Expand Down
28 changes: 28 additions & 0 deletions include/picotorrent/client/qr/bit_buffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <vector>

namespace picotorrent
{
namespace client
{
namespace qr
{
class bit_buffer
{
public:
bit_buffer();

std::vector<char> get_buffer();
int get_length_in_bits();
void put(int num, int length);
void put(bool bit);

private:
std::vector<char> buffer_;
int inclements_;
int length_;
};
}
}
}
30 changes: 30 additions & 0 deletions include/picotorrent/client/qr/polynomial.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <string>
#include <vector>

namespace picotorrent
{
namespace client
{
namespace qr
{
class polynomial
{
public:
polynomial(const std::vector<int> &num);
polynomial(const std::vector<int> &num, int shift);

int get(int index) const;
int get_length() const;
polynomial multiply(const polynomial &other);
polynomial mod(const polynomial &other);
std::string to_string();
std::string to_log_string();

private:
std::vector<int> num_;
};
}
}
}
21 changes: 21 additions & 0 deletions include/picotorrent/client/qr/qr_8bit_byte.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <picotorrent/client/qr/qr_data.hpp>

namespace picotorrent
{
namespace client
{
namespace qr
{
class qr_8bit_byte : public qr_data
{
public:
qr_8bit_byte(const std::string &data);

int get_length();
void write(bit_buffer &buffer);
};
}
}
}
73 changes: 73 additions & 0 deletions include/picotorrent/client/qr/qr_code.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once

#include <memory>
#include <string>
#include <vector>

#include <picotorrent/client/qr/qr_data.hpp>

#define MODE_NUMBER (1<<0)
#define MODE_ALPHA_NUM (1<<1)
#define MODE_8BIT_BYTE (1<<2)
#define MODE_KANJI (1<<3)

namespace picotorrent
{
namespace client
{
namespace qr
{
class bit_buffer;
class qr_8bit_byte;
class rs_block;

class qr_code
{
public:
qr_code();

int get_type_number();
void set_type_number(int type_number);
int get_error_correct_level();
void set_error_correct_level(int error_correct_level);
void add_data(const std::string &data);
void add_data(const std::string &data, int mode);
void clear_data();
bool is_dark(int row, int col);
int get_module_count();
void make();

static std::vector<char> create_data(int type_number, int error_correct_level, std::vector<std::shared_ptr<qr_data>> &data);

protected:
void add_data(const std::shared_ptr<qr_data> &data);
int get_data_count();
qr_data& get_data(int index);

private:
struct module_val
{
bool value;
bool has_value;
};

int get_best_mask_pattern();
void make(bool test, int mask_pattern);
void map_data(const std::vector<char> &data, int mask_pattern);
void setup_position_adjust_pattern();
void setup_position_probe_pattern(int row, int col);
void setup_timing_pattern();
void setup_type_number(bool test);
void setup_type_info(bool test, int mask_pattern);

static std::vector<char> create_bytes(bit_buffer &buffer, std::vector<rs_block> &blocks);

int type_number_;
std::vector<std::vector<module_val>> modules_;
int module_count_;
int error_correct_level_;
std::vector<std::shared_ptr<qr_data>> qr_data_list_;
};
}
}
}
31 changes: 31 additions & 0 deletions include/picotorrent/client/qr/qr_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <string>

namespace picotorrent
{
namespace client
{
namespace qr
{
class bit_buffer;

class qr_data
{
public:
std::string get_data();
virtual int get_length() = 0;
int get_length_in_bits(int type);
int get_mode();
virtual void write(bit_buffer &buffer) = 0;

protected:
qr_data(int mode, const std::string &data);

private:
int mode_;
std::string data_;
};
}
}
}
17 changes: 17 additions & 0 deletions include/picotorrent/client/qr/qr_math.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

namespace picotorrent
{
namespace client
{
namespace qr
{
class qr_math
{
public:
static int gexp(int n);
static int glog(int n);
};
}
}
}
35 changes: 35 additions & 0 deletions include/picotorrent/client/qr/qr_util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <string>
#include <vector>

#include <picotorrent/client/qr/polynomial.hpp>

namespace picotorrent
{
namespace client
{
namespace qr
{
class qr_code;

class qr_util
{
public:
static std::string get_jis_encoding();
static std::vector<int> get_pattern_position(int type_number);
static int get_max_length(int type_number, int mode, int error_correct_level);
static polynomial get_error_correct_polynomial(int error_correct_length);
static bool get_mask(int mask_pattern, int i, int j);
static int get_lost_point(qr_code &qr);
static int get_mode(const std::string &m);
static int get_bch_type_info(int data);
static int get_bch_type_number(int data);
static int get_bch_digit(int data);

private:
qr_util() {}
};
}
}
}
29 changes: 29 additions & 0 deletions include/picotorrent/client/qr/rs_block.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <vector>

namespace picotorrent
{
namespace client
{
namespace qr
{
class rs_block
{
public:
rs_block(int total_count, int data_count);

int get_data_count();
int get_total_count();

static std::vector<rs_block> get_rs_blocks(int type_number, int error_correct_level);

private:
static std::vector<int> get_rs_block_table(int type_number, int error_correct_level);

int data_count_;
int total_count_;
};
}
}
}
39 changes: 39 additions & 0 deletions include/picotorrent/client/ui/dialogs/remote_qr_dialog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <memory>
#include <string>

#include <picotorrent/client/ui/dialogs/dialog_base.hpp>

namespace picotorrent
{
namespace client
{
namespace qr
{
class qr_code;
}
namespace ui
{
namespace dialogs
{
class remote_qr_dialog : public dialog_base
{
public:
remote_qr_dialog();
~remote_qr_dialog();

void set_data(const std::string &data);

protected:
BOOL on_command(int, WPARAM, LPARAM);
BOOL on_init_dialog();
BOOL on_notify(LPARAM);

private:
std::shared_ptr<qr::qr_code> qr_;
};
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace preferences
protected:
BOOL on_command(HWND hDlg, UINT uCtrlId, WPARAM wParam, LPARAM lParam);
void on_init_dialog();
bool on_notify(HWND hDlg, LPNMHDR nmhdr, LRESULT &result);

private:
void check_changed(HWND hDlg, UINT uCtrlId, UINT uCommand);
Expand Down
5 changes: 5 additions & 0 deletions include/picotorrent/client/ui/resources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define IDD_PREFERENCES_GENERAL 317
#define IDD_PREFERENCES_REMOTE 318
#define IDD_DETAILS_OPTIONS 319
#define IDD_REMOTE_QR 320

// Menus
#define ID_FILE_ADD_TORRENT 9001
Expand Down Expand Up @@ -114,6 +115,7 @@
#define ID_REMOTE_TOKEN 107
#define ID_REMOTE_CERT_TEXT 108
#define ID_REMOTE_CERT_PUBKEY 109
#define ID_REMOTE_SHOW_QR 110

// Advanced preferences dialog
#define ID_EXPERIMENTAL_GROUP 101
Expand Down Expand Up @@ -187,5 +189,8 @@
#define ID_PICOTORRENT_DESCRIPTION 104
#define ID_GITHUB_LINK 105

// Remote QR dialog
#define ID_QR_CONTROL 101

// Keyboard shortcuts
#define IDHOT_SELECT_ALL 1001
3 changes: 2 additions & 1 deletion lang/1033.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"uploaded": "Uploaded",
"storage_mode": "Storage mode",
"sparse": "Sparse",
"full": "Full"
"full": "Full",
"show_qr_code": "<a href=\"#\">Show QR code</a>",
}
}

0 comments on commit 65457b2

Please sign in to comment.