Back to index

unity  6.0.0
Model-inl.h
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
00018  */
00019 
00020 #ifndef UNITY_MODEL_INL_H
00021 #define UNITY_MODEL_INL_H
00022 
00023 #include <NuxCore/Logger.h>
00024 
00025 namespace unity
00026 {
00027 namespace dash
00028 {
00029 
00030 namespace
00031 {
00032 nux::logging::Logger _model_inl_logger("unity.dash.model");
00033 }
00034 
00035 template<class RowAdaptor>
00036 Model<RowAdaptor>::Model()
00037   : model_type_(ModelType::REMOTE)
00038 {
00039   Init();
00040 }
00041 
00042 template<class RowAdaptor>
00043 Model<RowAdaptor>::Model (ModelType model_type)
00044   : model_type_(model_type)
00045 {
00046   Init();
00047 
00048   if (model_type == ModelType::LOCAL)
00049     swarm_name = ":local";
00050 }
00051 
00052 template<class RowAdaptor>
00053 void Model<RowAdaptor>::Init ()
00054 {
00055   swarm_name.changed.connect(sigc::mem_fun(this, &Model<RowAdaptor>::OnSwarmNameChanged));
00056   count.SetGetterFunction(sigc::mem_fun(this, &Model<RowAdaptor>::get_count));
00057   seqnum.SetGetterFunction(sigc::mem_fun(this, &Model<RowAdaptor>::get_seqnum));
00058   model.SetGetterFunction(sigc::mem_fun(this, &Model<RowAdaptor>::get_model));
00059 }
00060 
00061 template<class RowAdaptor>
00062 void Model<RowAdaptor>::OnSwarmNameChanged(std::string const& swarm_name)
00063 {
00064   typedef glib::Signal<void, DeeModel*, DeeModelIter*> RowSignalType;
00065   typedef glib::Signal<void, DeeModel*, guint64, guint64> TransactionSignalType;
00066 
00067   LOG_DEBUG(_model_inl_logger) << "New swarm name: " << swarm_name;
00068 
00069   // Let the views clean up properly
00070   if (model_)
00071     dee_model_clear(model_);
00072 
00073   switch(model_type_)
00074   {
00075     case ModelType::LOCAL:
00076       model_ = dee_sequence_model_new();
00077       break;
00078     case ModelType::REMOTE:
00079       model_ = dee_shared_model_new(swarm_name.c_str());
00080       sig_manager_.Add(new TransactionSignalType(model_,
00081                                                  "begin-transaction",
00082                                                  sigc::mem_fun(this, &Model<RowAdaptor>::OnTransactionBegin)));
00083 
00084       sig_manager_.Add(new TransactionSignalType(model_,
00085                                                  "end-transaction",
00086                                                  sigc::mem_fun(this, &Model<RowAdaptor>::OnTransactionEnd)));
00087       break;
00088     default:
00089       LOG_ERROR(_model_inl_logger) <<  "Unexpected ModelType " << model_type_;
00090       break;
00091   }
00092 
00093   model.EmitChanged(model_);
00094 
00095 
00096   renderer_tag_ = dee_model_register_tag(model_, NULL);
00097 
00098   sig_manager_.Add(new RowSignalType(model_,
00099                                      "row-added",
00100                                      sigc::mem_fun(this, &Model<RowAdaptor>::OnRowAdded)));
00101 
00102   sig_manager_.Add(new RowSignalType(model_,
00103                                      "row-changed",
00104                                      sigc::mem_fun(this, &Model<RowAdaptor>::OnRowChanged)));
00105 
00106   sig_manager_.Add(new RowSignalType(model_,
00107                                      "row-removed",
00108                                      sigc::mem_fun(this, &Model<RowAdaptor>::OnRowRemoved)));
00109 }
00110 
00111 template<class RowAdaptor>
00112 Model<RowAdaptor>::~Model()
00113 {}
00114 
00115 template<class RowAdaptor>
00116 void Model<RowAdaptor>::OnRowAdded(DeeModel* model, DeeModelIter* iter)
00117 {
00118   RowAdaptor it(model, iter, renderer_tag_);
00119   row_added.emit(it);
00120 }
00121 
00122 template<class RowAdaptor>
00123 void Model<RowAdaptor>::OnRowChanged(DeeModel* model, DeeModelIter* iter)
00124 {
00125   RowAdaptor it(model, iter, renderer_tag_);
00126   row_changed.emit(it);
00127 }
00128 
00129 template<class RowAdaptor>
00130 void Model<RowAdaptor>::OnRowRemoved(DeeModel* model, DeeModelIter* iter)
00131 {
00132   RowAdaptor it(model, iter, renderer_tag_);
00133   row_removed.emit(it);
00134 }
00135 
00136 template<class RowAdaptor>
00137 void Model<RowAdaptor>::OnTransactionBegin(DeeModel* model, guint64 begin_seqnum64, guint64 end_seqnum64)
00138 {
00139   unsigned long long begin_seqnum, end_seqnum;
00140 
00141   begin_seqnum = static_cast<unsigned long long> (begin_seqnum64);
00142   end_seqnum = static_cast<unsigned long long> (end_seqnum64);
00143   begin_transaction.emit(begin_seqnum, end_seqnum);
00144 }
00145 
00146 template<class RowAdaptor>
00147 void Model<RowAdaptor>::OnTransactionEnd(DeeModel* model, guint64 begin_seqnum64, guint64 end_seqnum64)
00148 {
00149   unsigned long long begin_seqnum, end_seqnum;
00150 
00151   begin_seqnum = static_cast<unsigned long long> (begin_seqnum64);
00152   end_seqnum = static_cast<unsigned long long> (end_seqnum64);
00153   end_transaction.emit(begin_seqnum, end_seqnum);
00154 }
00155 
00156 template<class RowAdaptor>
00157 const RowAdaptor Model<RowAdaptor>::RowAtIndex(std::size_t index)
00158 {
00159   RowAdaptor it(model_,
00160                 dee_model_get_iter_at_row(model_, index),
00161                 renderer_tag_);
00162   return it;
00163 }
00164 
00165 template<class RowAdaptor>
00166 std::size_t Model<RowAdaptor>::get_count()
00167 {
00168   if (model_)
00169     return dee_model_get_n_rows(model_);
00170   else
00171     return 0;
00172 }
00173 
00174 template<class RowAdaptor>
00175 unsigned long long Model<RowAdaptor>::get_seqnum()
00176 {
00177   if (model_ && DEE_IS_SERIALIZABLE_MODEL ((DeeModel*) model_))
00178     return dee_serializable_model_get_seqnum(model_);
00179   else
00180     return 0;
00181 }
00182 
00183 template<class RowAdaptor>
00184 glib::Object<DeeModel> Model<RowAdaptor>::get_model()
00185 {
00186   return model_;
00187 }
00188 
00189 }
00190 }
00191 
00192 #endif