Skip to content

Commit

Permalink
Merge pull request #148 from lidaobing/issue_140
Browse files Browse the repository at this point in the history
[#140] fix crash on TransWindow::TerminateTransTask.
  • Loading branch information
lidaobing committed Feb 9, 2018
2 parents 2a622b8 + 3c17aa3 commit e645f4b
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 83 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [#119] check the return code of `setsockopt`.
* [#?] switch to GTK+3, and use `GtkApplication`.
* [#125] fix crash on UdpData::SomeoneSendmsg.
* [#140] fix crash on TransWindow::TerminateTransTask.
* [#132] fix file accepted when cancel the directory chooser dialog.

### refactor
Expand Down
2 changes: 1 addition & 1 deletion src/iptux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ add_library(iptux
utils.cpp
wrapper.cpp
WindowConfig.cpp
TransWindow.cpp TransWindow.h TransFileModel.cpp TransFileModel.h TransAbstract.cpp TransAbstract.h UiUtils.cpp UiUtils.h)
TransWindow.cpp TransWindow.h TransFileModel.cpp TransFileModel.h TransAbstract.cpp TransAbstract.h UiUtils.cpp UiUtils.h UiModels.h UiModels.cpp)


add_executable(libiptux_test
Expand Down
35 changes: 6 additions & 29 deletions src/iptux/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
#include "iptux/output.h"
#include "iptux/TransWindow.h"
#include "iptux/UiUtils.h"
#include "iptux/UiModels.h"

using namespace std;

namespace iptux {

static const int TRANS_TREE_MAX = 14;

/**
* 类构造函数.
*/
Expand Down Expand Up @@ -85,7 +84,7 @@ void MainWindow::CreateWindow() {
/* 创建主窗口 */
window = CreateMainWindow();
g_object_set_data(G_OBJECT(window), "iptux-config", &config);
g_object_set_data_full(G_OBJECT(window), "trans-model", CreateTransModel(),
g_object_set_data_full(G_OBJECT(window), "trans-model", trans_model_new(),
GDestroyNotify(g_object_unref));

gtk_container_add(GTK_CONTAINER(window), CreateAllArea());
Expand Down Expand Up @@ -430,7 +429,7 @@ void MainWindow::UpdateItemToTransTree(const TransFileModel& para) {
bool found = false;
if (gtk_tree_model_get_iter_first(model, &iter)) {
do {
gtk_tree_model_get(model, &iter, TRANS_TREE_MAX, &data, -1);
gtk_tree_model_get(model, &iter, TransModelColumn::PARA, &data, -1);
if (gpointer(&para) == data) {
found = true;
break;
Expand All @@ -439,15 +438,15 @@ void MainWindow::UpdateItemToTransTree(const TransFileModel& para) {
}
if (!found) {
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
gtk_list_store_set(GTK_LIST_STORE(model), &iter, TRANS_TREE_MAX, &para, -1);
gtk_list_store_set(GTK_LIST_STORE(model), &iter, TransModelColumn::PARA, &para, -1);
}

/**
* @note 鉴于参数值(*para)的原地址有可能会被重用, 所以当("data"==null)
* 时应该清空参数指针值,以防止其他后来项误认此项为自己的大本营.
*/
if (!para.getData()) {
gtk_list_store_set(GTK_LIST_STORE(model), &iter, TRANS_TREE_MAX, NULL, -1);
gtk_list_store_set(GTK_LIST_STORE(model), &iter, TransModelColumn::PARA, NULL, -1);
}

/* 重设数据 */
Expand Down Expand Up @@ -475,7 +474,7 @@ bool MainWindow::TransmissionActive() {
model = GTK_TREE_MODEL(g_object_get_data(G_OBJECT(window), "trans-model"));
if (gtk_tree_model_get_iter_first(model, &iter)) {
do {
gtk_tree_model_get(model, &iter, TRANS_TREE_MAX, &data, -1);
gtk_tree_model_get(model, &iter, TransModelColumn::PARA, &data, -1);
if (data) break;
} while (gtk_tree_model_iter_next(model, &iter));
}
Expand Down Expand Up @@ -749,28 +748,6 @@ GtkTreeModel *MainWindow::CreatePallistModel() {
return GTK_TREE_MODEL(model);
}

/**
* 文件传输树(trans-tree)底层数据结构.
* 14,0 status,1 task,2 peer,3 ip,4 filename,5 filelength,6 finishlength,7
* progress, 8 pro-text,9 cost,10 remain,11 rate,12,pathname,13 data,14 para \n
* 任务状态;任务类型;任务对端;文件名(如果当前是文件夹,该项指正在传输的文件夹内单个文件,
* 整个文件夹传输完成后,该项指向当前是文件夹);文件长度;完成长度;完成进度;
* 进度串;已花费时间;任务剩余时间;传输速度;带路径文件名(不显示);文件传输类;参数指针值
* \n
* @return trans-model
*/
GtkTreeModel *MainWindow::CreateTransModel() {
GtkListStore *model;

model = gtk_list_store_new(15, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER);

return GTK_TREE_MODEL(model);
}

/**
* 创建好友树(paltree).
* @param model paltree-model
Expand Down
1 change: 0 additions & 1 deletion src/iptux/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class MainWindow {

GtkTreeModel *CreatePaltreeModel();
GtkTreeModel *CreatePallistModel();
GtkTreeModel *CreateTransModel();
GtkWidget *CreatePaltreeTree(GtkTreeModel *model);
GtkWidget *CreatePallistTree(GtkTreeModel *model);

Expand Down
4 changes: 2 additions & 2 deletions src/iptux/RecvFileData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ void RecvFileData::UpdateUIParaToOver() {
.setRate("");
file->finishedsize = file->filesize;
}
para.setData(nullptr);
para.finish();
}

} // namespace iptux
} // namespace iptux
2 changes: 1 addition & 1 deletion src/iptux/SendFileData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ void SendFileData::UpdateUIParaToOver() {
.setRemain("")
.setRate("");
}
para.setData(nullptr);
para.finish();
}

} // namespace iptux
20 changes: 19 additions & 1 deletion src/iptux/TransFileModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
#include <glib.h>

namespace iptux {

TransFileModel::TransFileModel()
: fileLength(0),
finishedLength(0),
finished(false),
data(nullptr) {
}

TransFileModel& TransFileModel::setStatus(const std::string& value) {
status = value;
return *this;
Expand Down Expand Up @@ -60,10 +68,16 @@ TransFileModel& TransFileModel::setFilePath(const std::string& value) {
}

TransFileModel& TransFileModel::setData(TransAbstract* value) {
g_assert_nonnull(value);
data = value;
return *this;
}

void TransFileModel::finish() {
finished = true;
data = nullptr;
}

const std::string& TransFileModel::getStatus() const {
return status;
}
Expand Down Expand Up @@ -133,4 +147,8 @@ int64_t TransFileModel::getFileLength() const {
return fileLength;
}

}
bool TransFileModel::isFinished() const {
return finished;
}

}
11 changes: 11 additions & 0 deletions src/iptux/TransFileModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class TransAbstract;

class TransFileModel {
public:
TransFileModel();

TransFileModel &setStatus(const std::string &value);
TransFileModel &setTask(const std::string &value);
TransFileModel &setPeer(const std::string &value);
Expand All @@ -20,7 +22,14 @@ class TransFileModel {
TransFileModel &setRemain(const std::string &value);
TransFileModel &setRate(const std::string &value);
TransFileModel &setFilePath(const std::string &value);

/**
*
* @param value not null
* @return
*/
TransFileModel &setData(TransAbstract *value);
void finish();

const std::string &getStatus() const;
const std::string &getTask() const;
Expand All @@ -37,6 +46,7 @@ class TransFileModel {
const std::string &getRate() const;
const std::string &getFilePath() const;
TransAbstract *getData() const;
bool isFinished() const;
private:
std::string status;
std::string task;
Expand All @@ -49,6 +59,7 @@ class TransFileModel {
std::string remain;
std::string rate;
std::string filePath;
bool finished;
TransAbstract *data;
};

Expand Down
Loading

0 comments on commit e645f4b

Please sign in to comment.