// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
* Copyright (C) 2011 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Authored by: Neil Jagdish Patel
*/
#ifndef UNITY_MODEL_INL_H
#define UNITY_MODEL_INL_H
#include
namespace unity
{
namespace dash
{
namespace
{
nux::logging::Logger _model_inl_logger("unity.dash.model");
}
template
Model::Model()
: model_type_(ModelType::REMOTE)
{
Init();
}
template
Model::Model (ModelType model_type)
: model_type_(model_type)
{
Init();
if (model_type == ModelType::LOCAL)
swarm_name = ":local";
}
template
void Model::Init ()
{
swarm_name.changed.connect(sigc::mem_fun(this, &Model::OnSwarmNameChanged));
count.SetGetterFunction(sigc::mem_fun(this, &Model::get_count));
seqnum.SetGetterFunction(sigc::mem_fun(this, &Model::get_seqnum));
model.SetGetterFunction(sigc::mem_fun(this, &Model::get_model));
}
template
void Model::OnSwarmNameChanged(std::string const& swarm_name)
{
typedef glib::Signal RowSignalType;
typedef glib::Signal TransactionSignalType;
LOG_DEBUG(_model_inl_logger) << "New swarm name: " << swarm_name;
// Let the views clean up properly
if (model_)
dee_model_clear(model_);
switch(model_type_)
{
case ModelType::LOCAL:
model_ = dee_sequence_model_new();
break;
case ModelType::REMOTE:
model_ = dee_shared_model_new(swarm_name.c_str());
sig_manager_.Add(new TransactionSignalType(model_,
"begin-transaction",
sigc::mem_fun(this, &Model::OnTransactionBegin)));
sig_manager_.Add(new TransactionSignalType(model_,
"end-transaction",
sigc::mem_fun(this, &Model::OnTransactionEnd)));
break;
default:
LOG_ERROR(_model_inl_logger) << "Unexpected ModelType " << model_type_;
break;
}
model.EmitChanged(model_);
renderer_tag_ = dee_model_register_tag(model_, NULL);
sig_manager_.Add(new RowSignalType(model_,
"row-added",
sigc::mem_fun(this, &Model::OnRowAdded)));
sig_manager_.Add(new RowSignalType(model_,
"row-changed",
sigc::mem_fun(this, &Model::OnRowChanged)));
sig_manager_.Add(new RowSignalType(model_,
"row-removed",
sigc::mem_fun(this, &Model::OnRowRemoved)));
}
template
Model::~Model()
{}
template
void Model::OnRowAdded(DeeModel* model, DeeModelIter* iter)
{
RowAdaptor it(model, iter, renderer_tag_);
row_added.emit(it);
}
template
void Model::OnRowChanged(DeeModel* model, DeeModelIter* iter)
{
RowAdaptor it(model, iter, renderer_tag_);
row_changed.emit(it);
}
template
void Model::OnRowRemoved(DeeModel* model, DeeModelIter* iter)
{
RowAdaptor it(model, iter, renderer_tag_);
row_removed.emit(it);
}
template
void Model::OnTransactionBegin(DeeModel* model, guint64 begin_seqnum64, guint64 end_seqnum64)
{
unsigned long long begin_seqnum, end_seqnum;
begin_seqnum = static_cast (begin_seqnum64);
end_seqnum = static_cast (end_seqnum64);
begin_transaction.emit(begin_seqnum, end_seqnum);
}
template
void Model::OnTransactionEnd(DeeModel* model, guint64 begin_seqnum64, guint64 end_seqnum64)
{
unsigned long long begin_seqnum, end_seqnum;
begin_seqnum = static_cast (begin_seqnum64);
end_seqnum = static_cast (end_seqnum64);
end_transaction.emit(begin_seqnum, end_seqnum);
}
template
const RowAdaptor Model::RowAtIndex(std::size_t index)
{
RowAdaptor it(model_,
dee_model_get_iter_at_row(model_, index),
renderer_tag_);
return it;
}
template
std::size_t Model::get_count()
{
if (model_)
return dee_model_get_n_rows(model_);
else
return 0;
}
template
unsigned long long Model::get_seqnum()
{
if (model_ && DEE_IS_SERIALIZABLE_MODEL ((DeeModel*) model_))
return dee_serializable_model_get_seqnum(model_);
else
return 0;
}
template
glib::Object Model::get_model()
{
return model_;
}
}
}
#endif