DirectPrint/DirectPrintApp/src/directprintmodel.cpp
branchRCL_3
changeset 11 613a5ff70823
child 19 2275db202402
equal deleted inserted replaced
10:3f1fec088555 11:613a5ff70823
       
     1 /*
       
     2 * Copyright (c) 2010 Kanrikogaku Kenkyusho, Ltd.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Kanrikogaku Kenkyusho, Ltd. - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * This model class for DirectPrint data managemrnt
       
    16 *
       
    17 */
       
    18 
       
    19 #include <bautils.h>
       
    20 #include <eikenv.h>
       
    21 #include <s32file.h>
       
    22 #include <DirectPrintApp.rsg>
       
    23 
       
    24 #include "clog.h"
       
    25 #include "DirectPrintModel.h"
       
    26 #include "directprintprinterinforeader.h"
       
    27 
       
    28 CDirectPrintModel::CDirectPrintModel()
       
    29 	: iCurrentPrinter(-1)
       
    30 	, iCurrentBearer(-1)
       
    31 	, iCurrentProperty(-1)
       
    32 	, iMaxPrinterId(0)
       
    33 	{
       
    34 	}
       
    35 
       
    36 CDirectPrintModel::~CDirectPrintModel()
       
    37 	{
       
    38 	iPrinters.Close();
       
    39 	iBearers.Close();
       
    40 	iProperties.ResetAndDestroy();
       
    41 	delete iPrinterInfoReader;
       
    42 	}
       
    43 
       
    44 CDirectPrintModel* CDirectPrintModel::NewL()
       
    45 	{
       
    46 	CDirectPrintModel* self = CDirectPrintModel::NewLC();
       
    47 	CleanupStack::Pop(self);
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 CDirectPrintModel* CDirectPrintModel::NewLC()
       
    52 	{
       
    53 	CDirectPrintModel* self = new(ELeave) CDirectPrintModel();
       
    54 	CleanupStack::PushL(self);
       
    55 	self->ConstructL();
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 void CDirectPrintModel::ConstructL()
       
    60 	{
       
    61 	LOG("CDirectPrintModel::ConstructL BEGIN");
       
    62 	iFs = CEikonEnv::Static()->FsSession();
       
    63 
       
    64 	// Read setting data
       
    65 	InternalizeDataL();
       
    66 
       
    67 	iPrinterInfoReader = CDirectPrintPrinterInfoReader::NewL(*this);
       
    68 	iPrinterInfoReader->ReadPrinterInfoL();
       
    69 
       
    70 	LOG("CDirectPrintModel::ConstructL END");
       
    71 	}
       
    72 
       
    73 void CDirectPrintModel::InitializeDataL()
       
    74 	{
       
    75 	}
       
    76 
       
    77 void CDirectPrintModel::InternalizeDataL()
       
    78 	{
       
    79 	LOG("CDirectPrintModel::ConstructL BEGIN");
       
    80 	TFileName fileName;
       
    81 	CEikonEnv::Static()->ReadResourceAsDes16L(fileName, R_DIRECTPRINT_SETTING_FILE);
       
    82 
       
    83 	if (BaflUtils::FileExists(iFs, fileName))
       
    84 		{
       
    85 		LOG("CDirectPrintModel::ConstructL file exist");
       
    86 		RFileReadStream stream;
       
    87 		TInt err = stream.Open(iFs, fileName, EFileRead|EFileStream);
       
    88 
       
    89 		TInt count, i;
       
    90 		count = stream.ReadInt32L();
       
    91 		for (i=0; i<count; i++)
       
    92 			{
       
    93 			TDirectPrintPrinterData data;
       
    94 			data.InternalizeL(stream);
       
    95 			AddPrinterDataL(data);
       
    96 			if (iMaxPrinterId < data.iPrinterId)
       
    97 				{
       
    98 				iMaxPrinterId = data.iPrinterId;
       
    99 				}
       
   100 			}
       
   101 		iCurrentPrinter = stream.ReadInt32L();
       
   102 
       
   103 		count = stream.ReadInt32L();
       
   104 		for (i=0; i<count; i++)
       
   105 			{
       
   106 			TDirectPrintBearerData data;
       
   107 			data.InternalizeL(stream);
       
   108 			AddBearerDataL(data);
       
   109 			}
       
   110 		iCurrentBearer = stream.ReadInt32L();
       
   111 
       
   112 		count = stream.ReadInt32L();
       
   113 		for (i=0; i<count; i++)
       
   114 			{
       
   115 			CPrinterProperty* property = new(ELeave)CPrinterProperty();
       
   116 			CleanupStack::PushL(property);
       
   117 			property->InternalizeL(stream);
       
   118 			CleanupStack::Pop(property);
       
   119 			AddPrinterPropertyL(property);
       
   120 			}
       
   121 		iCurrentProperty = stream.ReadInt32L();
       
   122 
       
   123 		stream.Close();
       
   124 		}
       
   125 	LOG("CDirectPrintModel::ConstructL END");
       
   126 	}
       
   127 
       
   128 void CDirectPrintModel::ExternalizeDataL()
       
   129 	{
       
   130 	LOG("CDirectPrintModel::ExternalizeDataL START");
       
   131 	TFileName fileName;
       
   132 	CEikonEnv::Static()->ReadResourceAsDes16L(fileName, R_DIRECTPRINT_SETTING_FILE);
       
   133 
       
   134 	RFileWriteStream stream;
       
   135 	TInt err = stream.Replace(iFs, fileName, EFileWrite|EFileStream);
       
   136 
       
   137 	TInt count, i;
       
   138 	count = iPrinters.Count();
       
   139 	stream.WriteInt32L(count);
       
   140 	for (i=0; i<count; i++)
       
   141 		{
       
   142 		iPrinters[i].ExternalizeL(stream);
       
   143 		}
       
   144 	stream.WriteInt32L(iCurrentPrinter);
       
   145 
       
   146 	count = iBearers.Count();
       
   147 	stream.WriteInt32L(count);
       
   148 	for (i=0; i<count; i++)
       
   149 		{
       
   150 		iBearers[i].ExternalizeL(stream);
       
   151 		}
       
   152 	stream.WriteInt32L(iCurrentBearer);
       
   153 
       
   154 	count = iProperties.Count();
       
   155 	stream.WriteInt32L(count);
       
   156 	for (i=0; i<count; i++)
       
   157 		{
       
   158 		iProperties[i]->ExternalizeL(stream);
       
   159 		}
       
   160 	stream.WriteInt32L(iCurrentProperty);
       
   161 
       
   162 	stream.Close();
       
   163 	LOG("CDirectPrintModel::ExternalizeDataL END");
       
   164 	}
       
   165 
       
   166 void CDirectPrintModel::GetPrinterDataL(TInt aIndex, TDirectPrintPrinterData& aData)
       
   167 	{
       
   168 	aData = iPrinters[aIndex];
       
   169 	}
       
   170 
       
   171 void CDirectPrintModel::GetBearerDataL(TInt aIndex, TDirectPrintBearerData& aData)
       
   172 	{
       
   173 	aData = iBearers[aIndex];
       
   174 	}
       
   175 
       
   176 void CDirectPrintModel::GetCurrentBearerDataL(TDirectPrintBearerData& aData)
       
   177 	{
       
   178 	TDirectPrintPrinterData printer;
       
   179 	GetPrinterDataL(CurrentPrinter(), printer);
       
   180 
       
   181 	TInt count = BearerCount();
       
   182 	for (TInt i=0; i<count; i++)
       
   183 		{
       
   184 		TInt printerId = iBearers[i].iPrinterId;
       
   185 		if (printerId == printer.iPrinterId)
       
   186 			{
       
   187 			aData = iBearers[i];
       
   188 			break;
       
   189 			}
       
   190 		}
       
   191 	}
       
   192 
       
   193 void CDirectPrintModel::GetSelectedBearerDataL(TDirectPrintBearerData& aData)
       
   194 	{
       
   195 	TDirectPrintPrinterData printer;
       
   196 	GetPrinterDataL(SelectPrinter(), printer);
       
   197 
       
   198 	TInt count = BearerCount();
       
   199 	for (TInt i=0; i<count; i++)
       
   200 		{
       
   201 		TInt printerId = iBearers[i].iPrinterId;
       
   202 		if (printerId == printer.iPrinterId)
       
   203 			{
       
   204 			aData = iBearers[i];
       
   205 			break;
       
   206 			}
       
   207 		}
       
   208 	}
       
   209 
       
   210 CPrinterProperty* CDirectPrintModel::GetPrinterPropertyL(TInt aIndex)
       
   211 	{
       
   212 	return iProperties[aIndex];
       
   213 	}
       
   214 
       
   215 CPrinterProperty* CDirectPrintModel::GetCurrentPrinterPropertyL(TUid aAppUid)
       
   216 	{
       
   217 	CPrinterProperty* property = NULL;
       
   218 	TDirectPrintPrinterData printer;
       
   219 	GetPrinterDataL(CurrentPrinter(), printer);
       
   220 
       
   221 	TInt index, count;
       
   222 	count = PropertyCount();
       
   223 	for (index=0; index<count; index++)
       
   224 		{
       
   225 		TInt printerId = iProperties[index]->iPrinterId;
       
   226 		TUid uid = TUid::Uid(iProperties[index]->iApplicationId);
       
   227 		if ((printerId == printer.iPrinterId) &&
       
   228 			(uid == aAppUid))
       
   229 			{
       
   230 			if (iCurrentProperty != index)
       
   231 				{
       
   232 				iCurrentProperty = index;
       
   233 				}
       
   234 			property = iProperties[index];
       
   235 			break;
       
   236 			}
       
   237 		}
       
   238 
       
   239 	return property;
       
   240 	}
       
   241 
       
   242 TInt CDirectPrintModel::GetCurrentPrinterIdL()
       
   243 	{
       
   244 	TDirectPrintPrinterData printer;
       
   245 	GetPrinterDataL(CurrentPrinter(), printer);
       
   246 	return printer.iPrinterId;
       
   247 	}
       
   248 
       
   249 TInt CDirectPrintModel::CurrentPrinter()
       
   250 	{
       
   251 	return iCurrentPrinter;
       
   252 	}
       
   253 
       
   254 TInt CDirectPrintModel::CurrentBearer()
       
   255 	{
       
   256 	return iCurrentBearer;
       
   257 	}
       
   258 
       
   259 TInt CDirectPrintModel::CurrentProperty()
       
   260 	{
       
   261 	return iCurrentProperty;
       
   262 	}
       
   263 
       
   264 TInt CDirectPrintModel::SelectPrinter()
       
   265 	{
       
   266 	return iSelectPrinter;
       
   267 	}
       
   268 
       
   269 TInt CDirectPrintModel::PrinterCount()
       
   270 	{
       
   271 	return iPrinters.Count();
       
   272 	}
       
   273 
       
   274 TInt CDirectPrintModel::BearerCount()
       
   275 	{
       
   276 	return iBearers.Count();
       
   277 	}
       
   278 
       
   279 TInt CDirectPrintModel::PropertyCount()
       
   280 	{
       
   281 	return iProperties.Count();
       
   282 	}
       
   283 
       
   284 void CDirectPrintModel::SetPrinterDataL(TInt aIndex, const TDirectPrintPrinterData& aData)
       
   285 	{
       
   286 	if ((aIndex >= 0) && (iPrinters.Count() > aIndex))
       
   287 		{
       
   288 		iPrinters[aIndex] = aData;
       
   289 		}
       
   290 	else
       
   291 		{
       
   292 		iPrinters.AppendL(aData);
       
   293 		}
       
   294 	iPrinterInfoReader->ReadPrinterInfoL();
       
   295 	}
       
   296 
       
   297 void CDirectPrintModel::SetPrinterDataL(const TDirectPrintPrinterData& aData)
       
   298 	{
       
   299 	TBool isSet = EFalse;
       
   300 	TInt count = PrinterCount();
       
   301 	for (TInt i=0; i<count; i++)
       
   302 		{
       
   303 		TInt printerId = iPrinters[i].iPrinterId;
       
   304 		if (printerId == aData.iPrinterId)
       
   305 			{
       
   306 			iPrinters[i] = aData;
       
   307 			isSet = ETrue;
       
   308 			break;
       
   309 			}
       
   310 		}
       
   311 
       
   312 	if (!isSet)
       
   313 		{
       
   314 		iPrinters.AppendL(aData);
       
   315 		}
       
   316 	iPrinterInfoReader->ReadPrinterInfoL();
       
   317 	}
       
   318 
       
   319 void CDirectPrintModel::SetBearerDataL(TInt aIndex, const TDirectPrintBearerData& aData)
       
   320 	{
       
   321 	if ((aIndex >= 0) && (iBearers.Count() > aIndex))
       
   322 		{
       
   323 		iBearers[aIndex] = aData;
       
   324 		}
       
   325 	else
       
   326 		{
       
   327 		iBearers.AppendL(aData);
       
   328 		}
       
   329 	}
       
   330 
       
   331 void CDirectPrintModel::SetBearerDataL(const TDirectPrintBearerData& aData)
       
   332 	{
       
   333 	TBool isSet = EFalse;
       
   334 	TInt count = BearerCount();
       
   335 	for (TInt i=0; i<count; i++)
       
   336 		{
       
   337 		TInt printerId = iBearers[i].iPrinterId;
       
   338 		if (printerId == aData.iPrinterId)
       
   339 			{
       
   340 			iBearers[i] = aData;
       
   341 			isSet = ETrue;
       
   342 			break;
       
   343 			}
       
   344 		}
       
   345 
       
   346 	if (!isSet)
       
   347 		{
       
   348 		iBearers.AppendL(aData);
       
   349 		}
       
   350 	}
       
   351 
       
   352 void CDirectPrintModel::SetPrinterPropertyL(TInt aIndex, CPrinterProperty* aProperty)
       
   353 	{
       
   354 	if ((aIndex >= 0) && (iProperties.Count() > aIndex))
       
   355 		{
       
   356 		*(iProperties[aIndex]) = *aProperty;
       
   357 		delete aProperty;
       
   358 		}
       
   359 	else
       
   360 		{
       
   361 		iProperties.AppendL(aProperty);
       
   362 		}
       
   363 	}
       
   364 
       
   365 void CDirectPrintModel::SetPrinterPropertyL(CPrinterProperty* aProperty)
       
   366 	{
       
   367 	CPrinterProperty* property = GetCurrentPrinterPropertyL(TUid::Uid(aProperty->iApplicationId));
       
   368 	if (property)
       
   369 		{
       
   370 		TInt count1, count2;
       
   371 		count1 = property->iPropertyItems.Count();
       
   372 		count2 = aProperty->iPropertyItems.Count();
       
   373 		for (TInt i=0; i<count2; i++)
       
   374 			{
       
   375 			TBool isSet = EFalse;
       
   376 			for (TInt j=0; j<count1; j++)
       
   377 				{
       
   378 				if (aProperty->iPropertyItems[i].iItemId == property->iPropertyItems[j].iItemId)
       
   379 					{
       
   380 					(property->iPropertyItems[j]) = (aProperty->iPropertyItems[i]);
       
   381 					isSet = ETrue;
       
   382 					break;
       
   383 					}
       
   384 				}
       
   385 
       
   386 			if (!isSet)
       
   387 				{
       
   388 				property->iPropertyItems.AppendL(aProperty->iPropertyItems[i]);
       
   389 				}
       
   390 			}
       
   391 		delete aProperty;
       
   392 		}
       
   393 	else
       
   394 		{
       
   395 		iProperties.AppendL(aProperty);
       
   396 		}
       
   397 	}
       
   398 
       
   399 void CDirectPrintModel::SetCurrentPrinter(TInt aIndex)
       
   400 	{
       
   401 	iCurrentPrinter = aIndex;
       
   402 	}
       
   403 
       
   404 void CDirectPrintModel::SetCurrentBearer(TInt aIndex)
       
   405 	{
       
   406 	iCurrentBearer = aIndex;
       
   407 	}
       
   408 
       
   409 void CDirectPrintModel::SetCurrentProperty(TInt aIndex)
       
   410 	{
       
   411 	iCurrentProperty = aIndex;
       
   412 	}
       
   413 
       
   414 void CDirectPrintModel::SetSelectPrinter(TInt aIndex)
       
   415 	{
       
   416 	iSelectPrinter = aIndex;
       
   417 	}
       
   418 
       
   419 void CDirectPrintModel::AddPrinterDataL(const TDirectPrintPrinterData& aData)
       
   420 	{
       
   421 	iPrinters.AppendL(aData);
       
   422 	}
       
   423 
       
   424 void CDirectPrintModel::AddBearerDataL(const TDirectPrintBearerData& aData)
       
   425 	{
       
   426 	iBearers.AppendL(aData);
       
   427 	}
       
   428 
       
   429 void CDirectPrintModel::AddPrinterPropertyL(CPrinterProperty* aProperty)
       
   430 	{
       
   431 	iProperties.AppendL(aProperty);
       
   432 	}
       
   433 
       
   434 void CDirectPrintModel::RemovePrinterDataL(TInt aIndex)
       
   435 	{
       
   436 	if ((aIndex >= 0) && (iPrinters.Count() > aIndex))
       
   437 		{
       
   438 		iPrinters.Remove(aIndex);
       
   439 		}
       
   440 	}
       
   441 
       
   442 void CDirectPrintModel::RemoveBearerDataL(TInt aIndex)
       
   443 	{
       
   444 	if ((aIndex >= 0) && (iBearers.Count() > aIndex))
       
   445 		{
       
   446 		iBearers.Remove(aIndex);
       
   447 		}
       
   448 	}
       
   449 
       
   450 void CDirectPrintModel::RemovePrinterPropertyL(TInt aIndex)
       
   451 	{
       
   452 	if ((aIndex >= 0) && (iProperties.Count() > aIndex))
       
   453 		{
       
   454 		iProperties.Remove(aIndex);
       
   455 		}
       
   456 	}
       
   457 
       
   458 void CDirectPrintModel::MoveTopPrinterDataL(TInt aIndex)
       
   459 	{
       
   460 	if ((aIndex >= 0) && (iPrinters.Count() > aIndex))
       
   461 		{
       
   462 		TDirectPrintPrinterData data = iPrinters[aIndex];
       
   463 		iPrinters.Remove(aIndex);
       
   464 		iPrinters.Insert(data, 0);
       
   465 		}
       
   466 	iPrinterInfoReader->ReadPrinterInfoL();
       
   467 	}
       
   468 
       
   469 TUid CDirectPrintModel::SettingUIPluginUidL()
       
   470 	{
       
   471 	TDirectPrintPrinterData printer;
       
   472 	GetPrinterDataL(0, printer);
       
   473 
       
   474 	return iPrinterInfoReader->GetSettingUIPluginL(printer.iTypeName);
       
   475 	}
       
   476