/*!
 * This method restores the toolbar layouts from the current preference
 * scheme. There are 3 generic keys.
 * The Number of entries ((Base Key for Nr entries)+(TB name))
 * The ith ID ((Base Key for id)+(TB name)+(i))
 * The ith Flag ((Base Key for flag)+(TB name)+(i))
 */
bool  XAP_Toolbar_Factory::restoreToolbarsFromCurrentScheme(void)
{
//
// First delete the current layouts.
//
	UT_VECTOR_PURGEALL(XAP_Toolbar_Factory_vec *,m_vecTT);
	m_vecTT.clear();
//
// Get the current scheme
//
	XAP_Prefs *pPrefs = m_pApp->getPrefs();
	XAP_PrefsScheme *pScheme = pPrefs->getCurrentScheme(true);
	char buf[100];
	XAP_Toolbar_Factory_vec * pVec = NULL;
//
// Get number of toolbars hardwired into abiword.
//
	UT_uint32 numTB = G_N_ELEMENTS(s_ttTable);  // NO toolabrs
	UT_uint32 iTB,iLay;
	for(iTB=0; iTB< numTB;iTB++)
	{
		UT_String sTBBase = XAP_PREF_KEY_ToolbarNumEntries;
		const char * szCurName =  s_ttTable[iTB].m_name;
		sTBBase +=szCurName;
		const gchar * szNrEntries = NULL;
		UT_uint32 NrEntries = 0;
//
// Get Number of entries if the correct key exists. Otherwise use defaults.
//
		pScheme->getValue((const gchar *)sTBBase.c_str(),&szNrEntries);
		if(szNrEntries && *szNrEntries)
		{	
			pVec = new XAP_Toolbar_Factory_vec(szCurName);
			m_vecTT.addItem((void *) pVec);
			NrEntries = atoi(szNrEntries);
//
// Loop through this toolbar definition and restore it from the preferences
//		
			for(iLay =0; iLay< NrEntries;iLay++)
			{
//
// Restore id from the contructed key
//
				sTBBase = XAP_PREF_KEY_ToolbarID;
				sTBBase += szCurName;
				sprintf(buf,"%d",iLay);
				sTBBase += buf;
				const gchar * szCurId = NULL;
				pScheme->getValue((const gchar *)sTBBase.c_str(),&szCurId);
				if(szCurId == NULL)
				{
					continue;
				}
				UT_return_val_if_fail (szCurId && *szCurId, false);
				XAP_Toolbar_Id curId = (XAP_Toolbar_Id) atoi(szCurId);
//
// Here we should check whether the ID exists or not
// 
				EV_Toolbar_Action * pAction = m_pApp->getToolbarActionSet()->getAction(curId);
				if (pAction == NULL) {
					UT_DEBUGMSG (("Found an unknown toolbar item in prefs. Ignoring.\n"));
					continue;
				}
//
// Restore flags from the constructed key
//
				sTBBase = XAP_PREF_KEY_ToolbarFlag;
				sTBBase += szCurName;
				sprintf(buf,"%d",iLay);
				sTBBase += buf;
				const gchar * szCurFlag = NULL;
				pScheme->getValue((const gchar *)sTBBase.c_str(),&szCurFlag);
				if(szCurFlag != NULL)
				{
					EV_Toolbar_LayoutFlags curFlag = (EV_Toolbar_LayoutFlags) atoi(szCurFlag);
//
// Build element and add it into the Toolbar layout
//
					XAP_Toolbar_Factory_lt * plt = new XAP_Toolbar_Factory_lt;
					plt->m_id = curId;
					plt->m_flags = curFlag;
					pVec->add_lt(plt);
				}
			}
		}
//
// Use default hardwired layout
//
		else
		{
			pVec = new XAP_Toolbar_Factory_vec(&s_ttTable[iTB]);
			m_vecTT.addItem((void *) pVec);
		}
	}
	return true;
}