Back to index

im-sdk  12.3.91
IIIMP_IMState.cpp
Go to the documentation of this file.
00001 #include <config.h>
00002 #include <stdio.h>
00003 #include <sys/types.h>
00004 #include <pwd.h>
00005 #include "IMAccept.hh"
00006 #include "IIIMP_IMState.hh"
00007 #include "IIIMP_ICState.hh"
00008 #include "IIIMProtocol.hh"
00009 #include "IIIMPTrans.hh"
00010 #include "IIIMPUtil.hh"
00011 #include "IIIMP_hotkey_profile.hh"
00012 
00013 IIIMP_IMState::UserIMStateMap IIIMP_IMState::user_imstate_map;
00014 
00015 bool
00016 IIIMP_IMState::parse_user_name(
00017     const u16string& uname,
00018     u16string& username,
00019     u16string& password,
00020     string& hostname
00021 )
00022 {
00023     unsigned int pos_at, pos_sharp, pos_colon;
00024     u16string hname_u16;
00025 
00026     pos_at = uname.find('@');
00027     if (pos_at == u16string::npos) {
00028        hname_u16 = "";
00029        pos_sharp = uname.find('#');
00030        if (pos_sharp == u16string::npos) {
00031            username = uname;
00032            password = "";
00033        } else {
00034            username = uname.substr(0, pos_sharp);
00035            password = uname.substr(pos_sharp + 1);
00036        }
00037     } else {
00038        username = uname.substr(0, pos_at);
00039        pos_sharp = uname.find('#', pos_at);
00040        if (pos_sharp == u16string::npos) {
00041            hname_u16 = uname.substr(pos_at + 1);
00042            password = "";
00043        } else {
00044            hname_u16 = uname.substr(pos_at + 1, pos_sharp - pos_at - 1);
00045            password = uname.substr(pos_sharp + 1);
00046        }
00047     }
00048 
00049     if (!hname_u16.get_charstr()) return false;
00050     pos_colon = hname_u16.find(":");
00051     if (pos_colon == u16string::npos) {
00052       hostname = hname_u16.get_charstr();
00053     } else {
00054       hostname = (hname_u16.substr(0, pos_colon)).get_charstr();
00055     }
00056     UserHostPair user_host_pair = make_pair(username, std::string(hname_u16.get_charstr()));
00057     std::pair<const UserHostPair, IIIMP_IMState* > user_imstate_pair(user_host_pair, this);
00058     user_imstate_map.insert(user_imstate_pair);
00059 
00060     return true;
00061 }
00062 
00063 bool
00064 IIIMP_IMState::send(
00065     IIIMP_message *pmes,
00066     bool deletep
00067 )
00068 {
00069     if (!pmes) return false;
00070     // TODO...
00071     pimt->send(pmes);
00072     if (deletep)
00073        iiimp_message_delete(pimt->get_data_s(), pmes);
00074     return true;
00075 }
00076 
00077 bool
00078 IIIMP_IMState::send_protocol_version(
00079     int client_proto_version
00080 )
00081 {
00082     if (client_proto_version > get_current_protocol_version())
00083        client_proto_version = get_current_protocol_version();
00084 
00085     // For version 2 clients or older ones,
00086     // we must not send IM_PROTOCOL_VERSION.
00087     if (client_proto_version <= 2)
00088        return true;
00089 
00090     IIIMP_data_s *pdata_s = get_iiimptrans()->get_data_s();
00091     if (!send(iiimp_protocol_version_new(pdata_s, get_im_id(), client_proto_version),
00092              true))
00093        return false;
00094 
00095     return true;
00096 }
00097 
00098 bool
00099 IIIMP_IMState::send_trigger_keys()
00100 {
00101     int i;
00102     int profile_id = get_hotkey_profile()->get_default_hotkey_profile_id();
00103     const IMKeySpecList *keylist = get_hotkey_profile()->retrieve_trigger_keys(profile_id);
00104 
00105     if (keylist != NULL) {
00106        IIIMP_keyevent_list *pion;
00107        IIIMP_keyevent_list *pioff;
00108        IIIMP_keyevent *keys = new IIIMP_keyevent[keylist->size()];
00109        IIIMP_data_s *pdata_s = get_iiimptrans()->get_data_s();
00110        IMKeySpecList::const_iterator it;
00111 
00112        for (i = 0, it = keylist->begin(); it != keylist->end(), i < keylist->size(); it++, i++) {
00113            keys[i].keycode = it->get_keycode();
00114            keys[i].keychar = it->get_keychar();
00115            keys[i].modifier = it->get_modifier();
00116            keys[i].time_stamp = it->get_timestamp();
00117        }
00118        pion = iiimp_keyevent_list_new(pdata_s, i, keys);
00119        pioff = iiimp_keyevent_list_new(pdata_s, i, keys);
00120        delete[] keys;
00121        if (!pion || !pioff) {
00122            if (pion)
00123               iiimp_keyevent_list_delete(pdata_s, pion);
00124            if (pioff)
00125               iiimp_keyevent_list_delete(pdata_s, pioff);
00126            return false;
00127        }
00128        if (!send(iiimp_register_trigger_keys_new(pdata_s, get_im_id(), pion, pioff), true))
00129            return false;
00130 
00131        return true;
00132     }
00133 
00134     return false;
00135 }
00136 
00137 bool
00138 IIIMP_IMState::send_hotkeys(
00139     const u16string& curr_input_lang,
00140     HotKeyList *hlist
00141 )
00142 {
00143     IIIMP_data_s *pdata_s = get_iiimptrans()->get_data_s();
00144     HOTKEY_LIST *hklist = (HOTKEY_LIST *)NULL;
00145     int i, profile_id, num_profiles, n_hotkeys;
00146     IMKeySpecList onkeys;
00147     IMKeySpecList offkeys;
00148     IIIMP_card16 scope_and_profile_id;
00149     IMHotkeyProfileStruct *hkps = const_cast<IMHotkeyProfileStruct *>(get_imhandler()->get_hotkey_profiles(&num_profiles));
00150     IMHotkeyStruct *hks;
00151     HotKeyList *tmp;
00152 
00153     if (hlist != NULL) {
00154        for (tmp = hlist; tmp != NULL; tmp = tmp->next) {
00155            LOG_DEBUG("Registering a trigger key <%s[%d]>%s[%d]", tmp->hotkey->modifiers, tmp->hotkey->modmask, tmp->hotkey->key, tmp->hotkey->keycode);
00156            onkeys.push_back(IMKeySpec(tmp->hotkey->keycode, 0, tmp->hotkey->modmask, 0));
00157        }
00158     } else {
00159        get_imhandler()->get_triggerkeys(onkeys, offkeys);
00160        onkeys.push_back(IMKeySpec(IM_VK_KANJI, 0, 0, 0));
00161        onkeys.push_back(IMKeySpec(IM_VK_KANJI, 0, IM_ALT_MASK, 0));
00162        if (strstr(curr_input_lang.get_charstr(), (char *)"ja") ||
00163            strstr(curr_input_lang.get_charstr(), (char *)"ko")) {
00164            LOG_DEBUG("For ja/ko locale clients, Shift+space is enabled for hotkey\n");
00165            onkeys.push_back(IMKeySpec(IM_VK_SPACE, 0, IM_SHIFT_MASK, 0));
00166        }
00167     }
00168     hks = get_hotkey_profile()->get_super_hotkeys(onkeys, curr_input_lang.get_charstr(), &n_hotkeys);
00169     get_hotkey_profile()->init_hotkey_profile_list(hkps, num_profiles);
00170     get_hotkey_profile()->add_super_hotkey_profiles(curr_input_lang, hks, n_hotkeys);
00171     num_profiles = get_hotkey_profile()->get_num_profiles();
00172     for (int i = 0; i < n_hotkeys; i++) {
00173        free(hks[i].label);
00174        delete [] hks[i].keys;
00175     }
00176     delete [] hks;
00177 
00178     for (i = 0; i < num_profiles; i++) {
00179        hklist = get_hotkey_profile()->retrive_hotkeys(pdata_s, i, &profile_id);
00180        scope_and_profile_id = (GLOBAL_HOTKEY << 15) | profile_id;
00181        if (!send(iiimp_register_hotkeys_new(pdata_s, get_im_id(), scope_and_profile_id, hklist), true))
00182            return false;
00183        scope_and_profile_id = 0;
00184        hklist = (HOTKEY_LIST *)NULL;
00185     }
00186     return true;
00187 }
00188 
00189 bool
00190 IIIMP_IMState::switch_hotkey_profile()
00191 {
00192     IIIMP_data_s *pdata_s = get_iiimptrans()->get_data_s();
00193     IIIMP_card16 scope_and_profile_id;
00194     IIIMP_card16 profile_id = get_hotkey_profile()->get_default_hotkey_profile_id();
00195     LOG_DEBUG("Switching Hotkey Profile to default [%d]\n", profile_id);
00196     scope_and_profile_id = (GLOBAL_HOTKEY << 15) | profile_id;
00197     if (!send(iiimp_select_hotkey_profile_new(pdata_s, get_im_id(), scope_and_profile_id), true))
00198        return false;
00199     return true;
00200 }
00201 
00202 bool
00203 IIIMP_IMState::message_proc(
00204     void *x_pmes
00205 )
00206 {
00207     IIIMP_message *pmes = (IIIMP_message*) x_pmes;
00208     IMState *pimns;
00209 
00210     switch (pmes->opcode) {
00211       case IM_CONNECT:
00212       {
00213          IMAccept *pima = get_imaccept();
00214          IMHandler *pimh;
00215          const IMLangList *planglist;
00216          u16string username, password;
00217          string hostname;
00218          int proto_version = pmes->v.connect.protocol_version;
00219 
00220          if (!parse_user_name(CONV_IIIMP_STR(pmes->v.connect.user_name),
00221                             username,
00222                             password,
00223                             hostname))
00224              return false;
00225          pimh = pima->request_connect(pimt->get_fd(),
00226                                    username,
00227                                    hostname,
00228                                    password,
00229                                    IMAuthList());
00230          if (!pimh) break;
00231 
00232          // Authentication is completed.  Now start communication!
00233          send_protocol_version(proto_version);
00234 
00235          planglist = pimh->get_langlist(NULL);
00236 
00237          {
00238              IIIMP_string *phead, *pcur = NULL, *pstr;
00239              IMLangList::const_iterator it;
00240              u16string langid;
00241 
00242              phead = NULL;
00243              for (it = planglist->begin(); it != planglist->end(); it++) {
00244                 langid = it->get_id();
00245                 pstr = iiimp_string_new(pimt->get_data_s(),
00246                                      langid.size(),
00247                                      langid.data());
00248                 // memory error
00249                 if (!pstr) break;
00250                 if (!phead) phead = pstr;
00251                 else pcur->next = pstr;
00252                 pcur = pstr;
00253              }
00254              if (!send(iiimp_connect_reply_new(pimt->get_data_s(),
00255                                           get_im_id(),
00256                                           phead),
00257                      true))
00258                 return false;
00259          }
00260          
00261          pimns = new IIIMP_IMState_Identified(this, pimh, proto_version);
00262 
00263          // send_trigger_keys();
00264 
00265          // send_hotkeys();
00266 
00267           // switch_hotkey_profile();
00268  
00269          change_state(*pimns);
00270 
00271          break;
00272       }
00273       default:
00274        LOG_ERROR("Invalid opcode:%d.  (Authentication is requied.)", pmes->opcode);
00275        return false;
00276     }
00277     return true;
00278 }
00279 
00280 void
00281 IIIMP_IMState::destroy()
00282 {
00283     UserIMStateMap::iterator it;
00284 
00285     for (it = IIIMP_IMState::user_imstate_map.begin(); it != IIIMP_IMState::user_imstate_map.end(); ++it) {
00286        if (it->second == this) {
00287            user_imstate_map.erase(it);
00288            break;
00289        }
00290     }
00291 
00292     delete get_iiimptrans();
00293     IMState::destroy();
00294 }
00295 
00296 IIIMP_IMState::IIIMP_IMState(
00297     IIIMProtocol *pimp,
00298     IIIMPTrans *x_pimt,
00299     int proto_version
00300 ) :
00301     IMState(1, pimp, NULL, proto_version)
00302 {
00303     pimt = x_pimt;
00304 }
00305 
00306 IIIMP_imattribute *
00307 IIIMP_IMState_Identified::
00308 create_object_descriptors()
00309 {
00310     const IMObjectWithDescList *podlist = get_imhandler()->get_imobjectdesclist();
00311 
00312     if (!podlist) return NULL;
00313 
00314     IIIMP_imattribute *pima = NULL;
00315     IIIMP_data_s *pdata_s = get_iiimptrans()->get_data_s();
00316     IIIMP_string *pidomain = NULL, *pihrn = NULL, *pisig = NULL, *piscope = NULL;
00317     IIIMP_object_descriptor *piodh, *piod = NULL, *piod2;
00318     IIIMP_card16 predefined;
00319 
00320     IMObjectWithDescList::const_iterator it;
00321     IMObjectWithDesc *pod;
00322 
00323     piodh = NULL;
00324     for (it = podlist->begin(); it != podlist->end(); it++) {
00325        pod = *it;
00326        if (pod->get_category() < 0) continue;
00327        predefined = convert_IMObject_type_to_iiimp_predefined_attribid(pod->get_type());
00328 
00329        pidomain = pihrn = pisig = piscope = NULL;
00330 
00331        pidomain = iiimp_string_new(pdata_s,
00332                                 pod->get_domain().size(),
00333                                 pod->get_domain().data());
00334        pihrn = iiimp_string_new(pdata_s,
00335                              pod->get_hrn().size(),
00336                              pod->get_hrn().data());
00337        pisig = iiimp_string_new(pdata_s,
00338                              pod->get_signature().size(),
00339                              pod->get_signature().data());
00340        piscope = iiimp_string_new(pdata_s,
00341                                pod->get_scope().size(),
00342                                pod->get_scope().data());
00343 
00344        if (!pidomain || !pihrn || !pisig || !piscope) {
00345            goto memory_error;
00346        }
00347        piod2 = iiimp_object_descriptor_new(pdata_s,
00348                                        pod->get_category(),
00349                                        pod->get_object_size(),
00350                                        predefined, pod->get_attribid(),
00351                                        pidomain, pihrn, pisig, piscope);
00352        if (!piod2) goto memory_error;
00353 
00354        if (!piodh) {
00355            piodh = piod2;
00356        } else {
00357            piod->next = piod2;
00358        }
00359        piod = piod2;
00360     }
00361 
00362     if (!piodh) return NULL;
00363 
00364     pima = iiimp_imattribute_object_descriptor_new(pdata_s,
00365                                              IIIMP_IMATTRIBUTE_OBJECT_DESCRIPTOR_LIST,
00366                                              0, piodh);
00367     if (!pima) goto memory_error;
00368 
00369     return pima;
00370 
00371 memory_error:
00372     if (pima) {
00373        iiimp_imattribute_delete(pdata_s, pima);
00374     } else {
00375        if (pidomain) iiimp_string_delete(pdata_s, pidomain);
00376        if (pihrn) iiimp_string_delete(pdata_s, pihrn);
00377        if (pisig) iiimp_string_delete(pdata_s, pisig);
00378        if (piscope) iiimp_string_delete(pdata_s, piscope);
00379        for (piod = piodh; piod; piod = piod->next) {
00380            iiimp_object_descriptor_delete(pdata_s, piod);
00381        }
00382     }
00383     // TODO!! we have to throw an exception.
00384     return NULL;
00385 }
00386 
00387 IIIMP_language*
00388 IIIMP_IMState_Identified::
00389 create_language_list(
00390     const IMLangList *plangs
00391 )
00392 {
00393     IIIMP_language *pil = NULL, *pil2, *pilh;
00394     IIIMP_string *pihrn, *piid;
00395     u16string langid;
00396     IIIMP_data_s *pdata_s = get_iiimptrans()->get_data_s();
00397     IMLangList::const_iterator it;
00398 
00399     if (!plangs) return NULL;
00400 
00401     pilh = NULL;
00402     for (it = plangs->begin(); it != plangs->end(); it++) {
00403        pihrn = piid = NULL;
00404 
00405        pihrn = iiimp_string_new(pdata_s,
00406                              it->get_hrn().size(),
00407                              it->get_hrn().data());
00408        if (!pihrn) goto memory_error;
00409 
00410        langid = it->get_id();
00411        piid = iiimp_string_new(pdata_s,
00412                             langid.size(),
00413                             langid.data());
00414        if (!piid) {
00415            iiimp_string_delete(pdata_s, pihrn);
00416            goto memory_error;
00417        }
00418 
00419        pil2 = iiimp_language_new(pdata_s, pihrn, piid);
00420        
00421        if (!pil2) {
00422            iiimp_string_delete(pdata_s, pihrn);
00423            iiimp_string_delete(pdata_s, piid);
00424            goto memory_error;
00425        }
00426 
00427        if (!pilh) {
00428            pilh = pil2;
00429        } else {
00430            pil->next = pil2;
00431        }
00432        pil = pil2;
00433     }
00434 
00435     return pilh;
00436 
00437 memory_error:
00438     if (pilh) iiimp_language_delete(pdata_s, pilh);
00439     // TODO!! we have to throw an exception.
00440     return NULL;
00441 }
00442 
00443 IIIMP_imeinfo *
00444 IIIMP_IMState_Identified::
00445 create_imeinfo_list(
00446     const IMImeInfoList *pimeinfos
00447 )
00448 {
00449     IIIMP_imeinfo *pil = NULL, *pil2, *pilh;
00450     IIIMP_string *pime_id, *pimename, *pversion, *pdescription, *pauthor, *pcopyright, *preserved1, *preserved2;
00451     u16string ime_id;
00452     IIIMP_data_s *pdata_s = get_iiimptrans()->get_data_s();
00453     IMImeInfoList::const_iterator it;
00454     int i = 0;
00455 
00456     if (!pimeinfos) return NULL;
00457 
00458     pilh = NULL;
00459     for (it = pimeinfos->begin(); it != pimeinfos->end(); it++) {
00460 
00461        ime_id = it->get_ime_id ();
00462         pime_id = iiimp_string_new(pdata_s,
00463                                 ime_id.size(),
00464                                 ime_id.data());
00465        if (!pime_id) goto memory_error;
00466 
00467         pimename = iiimp_string_new(pdata_s,
00468                                 it->get_imename().size(),
00469                                 it->get_imename().data());
00470        if (!pimename) goto memory_error;
00471 
00472         pversion = iiimp_string_new(pdata_s,
00473                                 it->get_version().size(),
00474                                 it->get_version().data());
00475        if (!pversion) goto memory_error;
00476 
00477         pdescription = iiimp_string_new(pdata_s,
00478                                 it->get_description().size(),
00479                                 it->get_description().data());
00480        if (!pdescription) goto memory_error;
00481 
00482         pauthor = iiimp_string_new(pdata_s,
00483                                 it->get_author().size(),
00484                                 it->get_author().data());
00485        if (!pauthor) goto memory_error;
00486 
00487         pcopyright = iiimp_string_new(pdata_s,
00488                                 it->get_copyright().size(),
00489                                 it->get_copyright().data());
00490        if (!pcopyright) goto memory_error;
00491 
00492         preserved1 = iiimp_string_new(pdata_s,
00493                                 it->get_reserved1().size(),
00494                                 it->get_reserved1().data());
00495        if (!preserved1) goto memory_error;
00496 
00497         preserved2 = iiimp_string_new(pdata_s,
00498                                 it->get_reserved2().size(),
00499                                 it->get_reserved2().data());
00500        if (!preserved2) goto memory_error;
00501 
00502         pil2 = iiimp_imeinfo_new(pdata_s, 
00503                             (int)it->get_enable(), 
00504                             pime_id,
00505                             pimename, 
00506                             pversion, 
00507                             pdescription, 
00508                             pauthor,
00509                             pcopyright, 
00510                             preserved1, 
00511                             preserved2);
00512 
00513         if (!pil2) {
00514             goto memory_error;
00515         }
00516 
00517         if (!pilh) {
00518             pilh = pil2;
00519         } else {
00520             pil->next = pil2;
00521         }
00522         pil = pil2;
00523     }
00524 
00525     return pilh;
00526 
00527 memory_error:
00528     if (pilh) iiimp_imeinfo_delete(pdata_s, pilh);
00529     // TODO!! we have to throw an exception.
00530     return NULL;
00531 }
00532 
00533 IIIMP_imattribute*
00534 IIIMP_IMState_Identified::
00535 create_input_method_descriptors()
00536 {
00537     const IMDescriptorList *pimdlist = get_imhandler()->get_imdesclist(NULL);
00538 
00539     if (!pimdlist) return NULL;
00540 
00541     IIIMP_imattribute *pima = NULL;
00542     IIIMP_data_s *pdata_s = get_iiimptrans()->get_data_s();
00543     IIIMP_string *pidname, *pihrn, *pidomain;
00544     IIIMP_inputmethod_descriptor *pimdh, *pimd = NULL, *pimd2;
00545     IIIMP_language *pil;
00546     IIIMP_imeinfo *imeinfo;
00547 
00548     IMDescriptorList::const_iterator it;
00549 
00550     pimdh = NULL;
00551 
00552     for (it = pimdlist->begin(); it != pimdlist->end(); it++) {
00553        pidname = pihrn = NULL;
00554 
00555        pidname = iiimp_string_new(pdata_s,
00556                                it->get_imname().size(),
00557                                it->get_imname().data());
00558 
00559        pihrn = iiimp_string_new(pdata_s,
00560                              it->get_hrn().size(),
00561                              it->get_hrn().data());
00562 
00563        /* Currently adopt an empty string
00564           as reverse domain name. */
00565        pidomain = iiimp_string_new(pdata_s, 0, NULL);
00566 
00567        if (!pidname || !pihrn || !pidomain) {
00568            goto memory_error;
00569        }
00570 
00571        pil = create_language_list(it->get_languages());
00572        imeinfo = create_imeinfo_list (it->get_imeinfos());
00573        pimd2 = iiimp_inputmethod_descriptor2_new(pdata_s,
00574                                             it->get_attribid(),
00575                                             pidname,
00576                                             pihrn,
00577                                             pil,
00578                                             pidomain,
00579                                             imeinfo);
00580 
00581        if (!pimd2) goto memory_error;
00582 
00583        if (!pimdh) {
00584            pimdh = pimd2;
00585        } else {
00586            pimd->next = pimd2;
00587        }
00588        pimd = pimd2;
00589     }
00590     if (!pimdh) return NULL;
00591 
00592     pima = iiimp_imattribute_inputmethod_descriptor_new(pdata_s,
00593                                                  IIIMP_IMATTRIBUTE_INPUT_METHOD_LIST,
00594                                                  0, pimdh);
00595     if (!pima) goto memory_error;
00596 
00597     return pima;
00598 
00599 memory_error:
00600     if (pima) {
00601        iiimp_imattribute_delete(pdata_s, pima);
00602     } else {
00603        if (pidname) iiimp_string_delete(pdata_s, pidname);
00604        if (pihrn) iiimp_string_delete(pdata_s, pihrn);
00605        if (pidomain) iiimp_string_delete(pdata_s, pidomain);
00606        for (pimd = pimdh; pimd; pimd = pimd->next) {
00607            iiimp_inputmethod_descriptor_delete(pdata_s, pimd);
00608        }
00609     }
00610     // TODO!! we have to throw an exception.
00611     return NULL;
00612 }
00613 
00614 bool
00615 IIIMP_IMState_Identified::
00616 set_data_to_client()
00617 {
00618     IIIMP_imattribute *pima, *pima_objdesc, *pima_imdesc, *pima_capability, *pima_last;
00619     IIIMP_card32_list * capability;
00620 
00621     pima_objdesc = create_object_descriptors();
00622 
00623     if (check_protocol_version(3))
00624        /* Send it only to clients of version 3 or later. */
00625        pima_imdesc = create_input_method_descriptors();
00626     else
00627        pima_imdesc = NULL;
00628 
00629     if (pima_objdesc) {
00630        pima = pima_objdesc;
00631        pima->next = pima_imdesc;
00632        pima_last = pima->next;
00633     } else {
00634        pima = pima_imdesc;
00635        pima_last = pima;
00636     }
00637 
00638     capability = iiimp_card32_list_bit_set(get_iiimptrans()->get_data_s(),
00639                                       NULL,
00640                                       IIIMP_CAPABILITY_FILE_OPERATION);
00641     capability = iiimp_card32_list_bit_set(get_iiimptrans()->get_data_s(),
00642                                       capability,
00643                                       IIIMP_CAPABILITY_KEY_RELEASE);
00644     pima_capability =
00645        iiimp_imattribute_capability_new(
00646            get_iiimptrans()->get_data_s(), IIIMP_IMATTRIBUTE_CAPABILITY,
00647            0, capability);
00648     if (NULL == pima) {
00649        pima = pima_capability;
00650        pima_last = pima;
00651     } else {
00652        pima_last->next = pima_capability;
00653        pima_last = pima_last->next;
00654     }
00655 
00656     if (!send(iiimp_setimvalues_new(get_iiimptrans()->get_data_s(),
00657                                 get_im_id(), pima), true))
00658        return false;
00659     return true;
00660 }
00661 
00662 IIIMP_imattribute*
00663 IIIMP_IMState_Identified::get_imattribute(
00664     IIIMP_card16 id
00665 )
00666 {
00667     IIIMP_imattribute *pimattr;
00668     IIIMP_data_s *pdata_s = get_iiimptrans()->get_data_s();
00669     IMObject *pobj = IMObjectMgr::get_instance()->get_object_from_attribid(id);
00670     if (!pobj || !pobj->downloadablep()) return NULL;
00671     switch (pobj->get_type()) {
00672       case IMObject::BINARY_GUI:
00673       case IMObject::BINARY_LWE:
00674       {
00675          IIIMP_binaryfile_object *pbobj;
00676          IIIMP_string *pipath;
00677          IMObjectWithDesc *pod = (IMObjectWithDesc*) pobj;
00678 
00679          pipath = iiimp_string_new(pdata_s,
00680                                 pod->get_path().size(),
00681                                 pod->get_path().data());
00682          if (!pipath) goto memory_error;
00683          pbobj = iiimp_binaryfile_object_new(pdata_s, pipath);
00684          if (!pbobj) {
00685              iiimp_string_delete(pdata_s, pipath);
00686              goto memory_error;
00687          }
00688          if (pobj->get_type() == IMObject::BINARY_GUI) {
00689              pimattr = iiimp_imattribute_binary_gui_object_new(pdata_s,
00690                                                         id, id,
00691                                                         pbobj);
00692          } else {
00693              pimattr = iiimp_imattribute_binary_light_weight_engine_new(pdata_s,
00694                                                                 id, id,
00695                                                                 pbobj);
00696          }
00697          if (!pimattr) {
00698              iiimp_binaryfile_object_delete(pdata_s, pbobj);
00699              goto memory_error;
00700          }
00701       }
00702       break;
00703       case IMObject::JAVA_GUI:
00704       case IMObject::JAVA_LWE:
00705       {
00706          IIIMP_jarfile_object *pjobj;
00707          IIIMP_string *piclassnamesh, *piclassnames = NULL, *piclassnames2;
00708          IMObjectWithDesc *pod = (IMObjectWithDesc*) pobj;
00709 
00710          const CARD8BIT *pbr = pod->get_binary_representation();
00711          if (!pbr) return NULL;
00712 
00713          piclassnamesh = NULL;
00714          const list<u16string> &classes = pod->get_classes();
00715          list<u16string>::const_iterator it;
00716          for (it = classes.begin(); it != classes.end(); it++) {
00717              piclassnames2 = iiimp_string_new(pdata_s,
00718                                           it->size(),
00719                                           it->data());
00720              if (!piclassnames2) {
00721                 if (piclassnamesh)
00722                     iiimp_string_delete(pdata_s, piclassnamesh);
00723                 goto memory_error;
00724              }
00725              if (!piclassnamesh) {
00726                 piclassnamesh = piclassnames2;
00727              } else {
00728                 piclassnames->next = piclassnames2;
00729              }
00730              piclassnames = piclassnames2;
00731          }
00732          pjobj = iiimp_jarfile_object_new(pdata_s, piclassnamesh,
00733                                       pod->get_object_size(),
00734                                       pbr);
00735          if (!pjobj) {
00736              iiimp_string_delete(pdata_s, piclassnamesh);
00737              goto memory_error;
00738          }
00739          if (pobj->get_type() == IMObject::JAVA_GUI) {
00740              pimattr = iiimp_imattribute_jar_gui_object_new(pdata_s, id, id, pjobj);
00741          } else {
00742              pimattr = iiimp_imattribute_jar_light_weight_engine_object_new(pdata_s,
00743                                                                     id, id,
00744                                                                     pjobj);
00745          }
00746          if (!pimattr) {
00747              iiimp_jarfile_object_delete(pdata_s, pjobj);
00748              goto memory_error;
00749          }
00750       }
00751       break;
00752 
00753       default:
00754        return NULL;
00755     }
00756 
00757     return pimattr;
00758 
00759 memory_error:
00760     // TODO: throw exception.
00761     return NULL;
00762 }
00763 
00764 bool
00765 IIIMP_IMState_Identified::message_proc(
00766     void *message
00767 )
00768 {
00769     IIIMP_message *pmes = (IIIMP_message*) message;
00770 
00771     switch (pmes->opcode) {
00772       case IM_GETIMVALUES:
00773       {
00774          IIIMP_imattribute *pimattrh, *pimattr = NULL, *pimattr2;
00775          IIIMP_card16_list *pcl = pmes->v.getimvalues.attr_list;
00776          IIIMP_card16 *ptr = pcl->ptr;
00777          pimattrh = NULL;
00778          for (int i = 0; i < pcl->count; i++, ptr++) {
00779              pimattr2 = get_imattribute(*ptr);
00780              if (!pimattr2) continue;
00781              if (!pimattrh) {
00782                 pimattrh = pimattr2;
00783              } else {
00784                 pimattr->next = pimattr2;
00785              }
00786              pimattr = pimattr2;
00787          }
00788          if (!send(iiimp_getimvalues_reply_new(get_iiimptrans()->get_data_s(),
00789                                           get_im_id(),
00790                                           pimattrh),
00791                   true))
00792              return false;
00793          break;
00794       }
00795       break;
00796       case IM_SETIMVALUES:
00797       {
00798          IIIMP_imattribute *pima = pmes->v.setimvalues.attr_list;
00799          switch(pima->id) {
00800            case IIIMP_IMATTRIBUTE_CLIENT_DESCRIPTOR:
00801            {
00802               IIIMP_client_descriptor *pcd = pima->value.client_descriptor;
00803 
00804               IMHandler::ClientAttrList attrs;
00805               if (pcd->type) {
00806                   attrs.push_back(IMHandler::ClientAttr(IMHandler::CLIENT_NAME,
00807                                                    CONV_IIIMP_STR(pcd->type)));
00808               }
00809               if (pcd->os_name) {
00810                   attrs.push_back(IMHandler::ClientAttr(IMHandler::OS_NAME,
00811                                                    CONV_IIIMP_STR(pcd->os_name)));
00812               }
00813               if (pcd->arch) {
00814                   attrs.push_back(IMHandler::ClientAttr(IMHandler::OS_ARCH,
00815                                                    CONV_IIIMP_STR(pcd->arch)));
00816               }
00817               if (pcd->version) {
00818                   attrs.push_back(IMHandler::ClientAttr(IMHandler::OS_VERSION,
00819                                                    CONV_IIIMP_STR(pcd->version)));
00820               }
00821               if (pcd->X_display_name) {
00822                   attrs.push_back(IMHandler::ClientAttr(IMHandler::X_DISPLAY_NAME,
00823                                                    CONV_IIIMP_STR(pcd->X_display_name)));
00824               }
00825               if (pcd->X_server_vendor) {
00826                   attrs.push_back(IMHandler::ClientAttr(IMHandler::X_SERVER_VENDOR,
00827                                                    CONV_IIIMP_STR(pcd->X_server_vendor)));
00828               }
00829 
00830               if (!(get_imhandler()->set_client_info(attrs)))
00831                   return false;
00832 
00833               set_data_to_client();
00834 
00835               if (!send(iiimp_setimvalues_reply_new(get_iiimptrans()->get_data_s(),
00836                                                 get_im_id()),
00837                        true))
00838                   return false;
00839               break;
00840            }
00841 
00842            default:
00843             LOG_ERROR("Unknown attribid:%d.", pima->id);
00844             return false;
00845          }
00846       }
00847       break;
00848 
00849       case IM_CREATEIC:
00850       {
00851          IIIMP_icattribute *pattr = pmes->v.createic.attr_list;
00852          ICAttribute icattr = convert_iiimp_icattr(pattr);
00853          ICHandler *pich = get_imhandler()->createic(icattr);
00854          if (!pich) {
00855              LOG_ERROR("Fail to create new ic (ICHandler).");
00856              return false;
00857          }
00858          CARD16BIT ic_id;
00859          if (!new_ic_id(ic_id)) return false;
00860          IIIMP_ICState *pics = IIIMP_ICState::create(ic_id, this, pich);
00861          if (!pics) {
00862              pich->destroy(NULL);
00863              LOG_ERROR("Fail to create new ic (IIIMP_ICState).");
00864              return false;
00865          }
00866          add_icstate(pics);
00867 
00868          if (!send(iiimp_createic_reply_new(get_iiimptrans()->get_data_s(),
00869                                         get_im_id(),
00870                                         ic_id),
00871                   true))
00872              return false;
00873 
00874           u16string curr_input_lang = icattr.get_inputlanguage();
00875 
00876           if (!get_register_hotkey_flag()) {
00877              iml_desktop_t *idt = pich->get_current_desktop();
00878              IIIMLEXMLConf *uconf = NULL;
00879              HotKeyList *hlist = NULL;
00880              string conffile = ".iiim/le.xml.conf";
00881 
00882 #if 0 /* disable reading the user-specific configuration ATM */
00883              /* load the user-specific configuration */
00884              uconf = iiim_le_xmlconf_new(conffile.c_str());
00885              if (uconf) {
00886                 iiim_le_xmlconf_load_with_nsio(uconf, idt);
00887                 hlist = iiim_le_xmlconf_get_hotkey_list(uconf, curr_input_lang.get_charstr());
00888              }
00889 #endif
00890              if (hlist == NULL) {
00891                 /* try to read the global configuration */
00892                 if (uconf)
00893                     iiim_le_xmlconf_free(uconf);
00894                 uconf = iiim_le_xmlconf_new(XMLCONFDIR "/le.xml.conf");
00895                 if (uconf) {
00896                        iiim_le_xmlconf_load_file(uconf);
00897                        hlist = iiim_le_xmlconf_get_hotkey_list(uconf, curr_input_lang.get_charstr());
00898                 }
00899              }
00900              send_hotkeys(curr_input_lang, hlist);
00901              send_trigger_keys();
00902              iiim_le_xmlconf_free(uconf);
00903 
00904              switch_hotkey_profile();
00905              set_register_hotkey_flag(true);
00906           }
00907       }
00908       break;
00909 
00910       case IM_DISCONNECT:
00911       {
00912          // reset_state() will delete `this'.
00913          bool flag;
00914          CARD16BIT im_id = get_im_id();
00915          IIIMPTrans *ptrans = get_iiimptrans();
00916 
00917          cleanup_ic();
00918          
00919          flag = send(iiimp_disconnect_reply_new(ptrans->get_data_s(),
00920                                            im_id),
00921                     true);
00922          reset_state();
00923          if (!flag) return false;
00924       }
00925       break;
00926 
00927       case IM_SETIMVALUES_REPLY:
00928        // reply message.  Currently, simply ignore it.
00929        break;
00930 
00931       default:
00932        LOG_ERROR("Invalid opcode:%d.  (Authentication is requied.)", pmes->opcode);
00933        return false;
00934     }
00935     return true;
00936 }
00937 
00938 IIIMP_IMState_Identified::IIIMP_IMState_Identified(
00939     IIIMP_IMState *pimt,
00940     IMHandler *pimh,
00941     int proto_version
00942 ) : IIIMP_IMState(*pimt, proto_version)
00943 {
00944     set_imhandler(pimh);
00945 }
00946 
00947 IIIMP_IMState *
00948 IIIMP_IMState::get_imstate_from_desktop(
00949     iml_desktop_t *desktop
00950 )
00951 {
00952     UserIMStateMap::iterator it;
00953 
00954     for (it = IIIMP_IMState::user_imstate_map.begin(); it != IIIMP_IMState::user_imstate_map.end(); ++it) {
00955       const UserHostPair& p = it->first;
00956       u16string username = p.first;
00957       string hostname = p.second;
00958       unsigned int pos_colon;
00959       string d = string(desktop->display_id);
00960       pos_colon = d.find(':');
00961       string display_id = d.substr(pos_colon, d.size());
00962       string hnameDpy1 = string(desktop->host_name) + string(desktop->display_id);
00963       string hnameDpy = string(desktop->host_name) + string(display_id);
00964       if (strcmp(desktop->user_name, username.get_charstr()) == 0 &&
00965          strcmp(hnameDpy.c_str(), hostname.c_str()) == 0) {
00966        return it->second;
00967       }
00968     }
00969 
00970     return NULL;
00971 }
00972 
00973 int 
00974 open_ns(
00975     iml_desktop_t *desktop,
00976     int ns_id,
00977     char *path,
00978     int oflag,
00979     mode_t mode,
00980     int *ns_errno
00981 )
00982 {
00983     IIIMP_message *send_message;
00984     IIIMP_message *receive_message;
00985     IIIMP_open *iiimp_open;
00986     IIIMP_utf8string *iiimp_fname;
00987     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
00988     *ns_errno = 0;
00989 
00990     if (xims == NULL)
00991        return -1;
00992     iiimp_fname = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
00993                                    (strlen(path) + 1),
00994                                    (const IIIMP_card8 *)path);
00995     if (!iiimp_fname) return -1;
00996 
00997     iiimp_open = iiimp_open_new(xims->get_iiimptrans()->get_data_s(),
00998                             ns_id,
00999                             mode,
01000                             iiimp_fname);
01001 
01002     send_message = iiimp_file_operation_new(
01003        xims->get_iiimptrans()->get_data_s(),
01004        xims->get_im_id(),
01005        IIIMP_FILE_OPERATION_TYPE_OPEN,
01006        iiimp_open);
01007 
01008     if(!xims->send(send_message, true)) return -1;
01009 
01010     for (;;) {
01011        receive_message = xims->get_iiimptrans()->receive();
01012         if (receive_message) {
01013            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01014               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01015               break;
01016            } else {
01017               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01018            }
01019         }
01020     }
01021 
01022     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_OPEN_REPLY) {
01023        IIIMP_open_reply *p = (IIIMP_open_reply *)receive_message->v.file_operation_reply.value;
01024        *ns_errno = p->ns_errno;
01025        return p->ns_id;
01026     }
01027     return -1;
01028 }
01029 
01030 int 
01031 read_ns(
01032     iml_desktop_t *desktop,
01033     int ns_id,
01034     void *ptr,
01035     size_t size,
01036     int *ns_errno
01037 )
01038 {
01039     IIIMP_message *send_message;
01040     IIIMP_message *receive_message;
01041     IIIMP_read *iiimp_read;
01042     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01043     *ns_errno = 0;
01044 
01045     if (xims == NULL)
01046        return -1;
01047     iiimp_read = iiimp_read_new(xims->get_iiimptrans()->get_data_s(), 
01048                             ns_id, size);
01049     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01050                                        xims->get_im_id(),
01051                                        IIIMP_FILE_OPERATION_TYPE_READ,
01052                                        iiimp_read);
01053     if(!xims->send(send_message, true)) return -1;
01054 
01055     for (;;) {
01056        receive_message = xims->get_iiimptrans()->receive();
01057        if (receive_message) {
01058            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01059               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01060               break;
01061            } else {
01062                xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01063            }
01064        }
01065     }
01066     if (!ptr) return -1;
01067 
01068     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_READ_REPLY) {
01069        IIIMP_read_reply *p = (IIIMP_read_reply *)receive_message->v.file_operation_reply.value;
01070        memcpy((char *)ptr, (char *)p->object, p->size);
01071        *ns_errno = p->ns_errno;
01072        return p->size;
01073     }
01074     return -1;
01075 }
01076 
01077 int
01078 write_ns(
01079     iml_desktop_t *desktop,
01080     int ns_id,
01081     void *ptr,
01082     size_t size,
01083     int *ns_errno
01084 )
01085 {
01086     IIIMP_message *send_message;
01087     IIIMP_message *receive_message;
01088     IIIMP_write *iiimp_write;
01089     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01090     *ns_errno = 0;
01091 
01092     if (xims == NULL)
01093        return -1;
01094     iiimp_write = iiimp_write_new(xims->get_iiimptrans()->get_data_s(),
01095                                   ns_id, size, ptr);
01096 
01097     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(), xims->get_im_id(), IIIMP_FILE_OPERATION_TYPE_WRITE, iiimp_write);
01098 
01099     if(!xims->send(send_message, true)) return -1;
01100 
01101     for (;;) {
01102         receive_message = xims->get_iiimptrans()->receive();
01103         if (receive_message) {
01104            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01105               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01106               break;
01107            } else {
01108               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01109            }
01110         }
01111     }
01112     if (!ptr) return -1;
01113 
01114     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_WRITE_REPLY) {
01115        IIIMP_write_reply *p = (IIIMP_write_reply *)receive_message->v.file_operation_reply.value;
01116        *ns_errno = p->ns_errno;
01117        return p->size;
01118     }
01119     
01120     return -1;
01121 }
01122 
01123 int
01124 update_supported_langlist_for_le(
01125     iml_desktop_t *desktop,
01126     IMLEName *lename,
01127     IMLocale *locales,
01128     int nLocales
01129 )
01130 {
01131     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01132 
01133     if (xims == NULL)
01134        return false;
01135     // Update LEMgr's IMDescriptorList
01136 
01137     xims->get_imhandler()->update_imdesclist(lename, locales, nLocales);
01138 
01139     // TODO!! Send the updated IMDescriptorList to Client using IM_SETIMVALUES
01140     // IIIMP_IMState_Identified *xims_id = new IIIMP_IMState_Identified(xims, xims->get_imhandler(), xims->get_current_protocol_version());
01141     // xims_id->set_data_to_client();
01142     // receive_message = xims->get_iiimptrans()->receive();
01143 
01144     return true;
01145 }
01146 
01147 // Method to switch hotkey profile from LE
01148 
01149 int 
01150 switch_le_profile(
01151     iml_session_t *s,
01152     int le_profile_id, 
01153     IMLEName *lename
01154 )   
01155 {   
01156     static int num = 0;
01157     IIIMP_message *send_message;
01158     ICState *pics = ((IMLExec_ICState *)s->SessionContext)->get_icstate();
01159     IIIMP_IMState *xims = (IIIMP_IMState *)pics->get_imstate();
01160     int default_profile_id;
01161     int profile_id;
01162     IIIMP_card16 scope_and_profile_id;
01163 
01164     if (xims == NULL) {
01165        LOG_DEBUG("IIIMP_IMState: xims is NULL \n");
01166        return false;
01167     }
01168     default_profile_id = xims->get_hotkey_profile()->get_default_hotkey_profile_id();
01169     profile_id = xims->get_hotkey_profile()->get_unique_profile_id(le_profile_id, lename);
01170     
01171     if (profile_id < 0) {
01172        profile_id = default_profile_id;
01173     }
01174 
01175     LOG_DEBUG("IIIMP_IMState: xims [0x%x], Switching HotkeyProfile [%d]\n", xims, num++);
01176     LOG_DEBUG("IIIMP_IMState: LE [%s] switching profile to [%d]\n",lename->id, profile_id);
01177 
01178     // Switch to LE's profile
01179     scope_and_profile_id = (GLOBAL_HOTKEY << 15) | profile_id;
01180     send_message = iiimp_select_hotkey_profile_new(xims->get_iiimptrans()->get_data_s(), xims->get_im_id(), scope_and_profile_id);
01181     if (!xims->send(send_message, true)) return false;
01182 
01183     return true;
01184 }
01185 
01186 int 
01187 close_ns(
01188     iml_desktop_t *desktop,
01189     int ns_id,
01190     int *ns_errno
01191 )
01192 {
01193     IIIMP_message *send_message;
01194     IIIMP_message *receive_message;
01195     IIIMP_close *iiimp_close;
01196     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01197     *ns_errno = 0;
01198 
01199     if (xims == NULL)
01200        return -1;
01201     iiimp_close = iiimp_close_new(xims->get_iiimptrans()->get_data_s(),
01202                                   ns_id);
01203 
01204     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(), xims->get_im_id(), IIIMP_FILE_OPERATION_TYPE_CLOSE, iiimp_close);
01205     if(!xims->send(send_message, true)) return -1;
01206 
01207     for (;;) {
01208        receive_message = xims->get_iiimptrans()->receive();
01209         if (receive_message) {
01210            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01211               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01212               break;
01213            } else {
01214               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01215            }
01216         }
01217     }
01218 
01219     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_CLOSE_REPLY) {
01220        IIIMP_close_reply *p = (IIIMP_close_reply *)receive_message->v.file_operation_reply.value;
01221        *ns_errno = p->ns_errno;
01222        return p->ns_id;
01223     }
01224     return 1;
01225 }
01226 
01227 char **
01228 opendir_ns(
01229     iml_desktop_t *desktop,
01230     int ns_id,
01231     char *path,
01232     int *nitems,
01233     int *pns_id,
01234     int ***d_reclen,
01235     int *ns_errno
01236 )
01237 {
01238     IIIMP_message *send_message;
01239     IIIMP_message *receive_message;
01240     IIIMP_opendir *iiimp_opendir;
01241     IIIMP_utf8string *iiimp_dirname;
01242     char **d_name;
01243     unsigned int i,j;
01244     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01245     *ns_errno = 0;
01246 
01247     if (xims == NULL)
01248        return NULL;
01249     iiimp_dirname = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01250                                          (strlen(path) + 1),
01251                                          (const IIIMP_card8 *)path);
01252     if (!iiimp_dirname) return (char **)NULL;
01253 
01254     iiimp_opendir = iiimp_opendir_new(xims->get_iiimptrans()->get_data_s(),
01255                                       ns_id, iiimp_dirname);
01256 
01257     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(), xims->get_im_id(), IIIMP_FILE_OPERATION_TYPE_OPENDIR, iiimp_opendir);
01258 
01259     if(!xims->send(send_message, true)) return (char **)NULL;
01260 
01261     for (;;) {
01262         receive_message = xims->get_iiimptrans()->receive();
01263         if (receive_message) {
01264            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01265               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01266               break;
01267            } else {
01268               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01269            }
01270         }
01271     }
01272 
01273     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_OPENDIR_REPLY) {
01274        IIIMP_opendir_reply *por = (IIIMP_opendir_reply *)receive_message->v.file_operation_reply.value;
01275        *nitems = por->nitems;
01276        *pns_id = por->ns_id;
01277        *ns_errno = por->ns_errno;
01278        if (*nitems > 0) {
01279            IIIMP_utf8string *p = por->d_name;
01280            d_name = (char **) calloc (*nitems, (sizeof (char *)));
01281            *d_reclen = (int **) calloc(*nitems, sizeof(int *));
01282            for (i=0; NULL != p; p=p->next, i++) {
01283               d_name[i] = (char *) calloc((p->len + 1), sizeof(char));
01284               (*d_reclen)[i] = (int *) calloc(1, sizeof(int));
01285               (*d_reclen)[i] = (int *) p->len;
01286               for (j=0; j<p->len; j++) {
01287                   d_name[i][j] = p->ptr[j];
01288               }
01289            }
01290            return d_name;
01291        }
01292     }
01293     return (char **)NULL;
01294 }
01295 
01296 int
01297 closedir_ns(
01298     iml_desktop_t *desktop,
01299     int ns_id,
01300     int *ns_errno
01301 )
01302 {
01303     IIIMP_message *send_message;
01304     IIIMP_message *receive_message;
01305     IIIMP_closedir *iiimp_closedir;
01306     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01307     *ns_errno = 0;
01308 
01309     if (xims == NULL)
01310        return -1;
01311     iiimp_closedir = iiimp_closedir_new(xims->get_iiimptrans()->get_data_s(),
01312                                         ns_id);
01313 
01314     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01315                                        xims->get_im_id(),
01316                                        IIIMP_FILE_OPERATION_TYPE_CLOSEDIR,
01317                                        iiimp_closedir);
01318 
01319     if(!xims->send(send_message, true)) return -1;
01320 
01321     for (;;) {
01322         receive_message = xims->get_iiimptrans()->receive();
01323         if (receive_message) {
01324            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01325               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01326               break;
01327            } else {
01328               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01329            }
01330         }
01331     }
01332     
01333     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_CLOSEDIR_REPLY) {
01334        IIIMP_closedir_reply *p = (IIIMP_closedir_reply *)receive_message->v.file_operation_reply.value;
01335        *ns_errno = p->ns_errno;
01336        return p->ns_id;
01337     }
01338     return -1;
01339 }
01340 
01341 int 
01342 mkdir_ns(
01343     iml_desktop_t *desktop,
01344     int ns_id,
01345     char *path,
01346     mode_t mode,
01347     int *ns_errno
01348 )
01349 {
01350     IIIMP_message *send_message;
01351     IIIMP_message *receive_message;
01352     IIIMP_mkdir *iiimp_mkdir;
01353     IIIMP_utf8string *iiimp_dirname;
01354     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01355     *ns_errno = 0;
01356 
01357     if (xims == NULL)
01358        return -1;
01359     iiimp_dirname = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01360                                          (strlen(path) + 1),
01361                                          (const IIIMP_card8 *)path);
01362     if (!iiimp_dirname) return -1;
01363 
01364     iiimp_mkdir = iiimp_mkdir_new(xims->get_iiimptrans()->get_data_s(),
01365                               ns_id, mode, iiimp_dirname);
01366 
01367     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01368                                        xims->get_im_id(),
01369                                        IIIMP_FILE_OPERATION_TYPE_MKDIR,
01370                                        iiimp_mkdir);
01371 
01372     if(!xims->send(send_message, true)) return -1;
01373 
01374     for (;;) {
01375        receive_message = xims->get_iiimptrans()->receive();
01376         if (receive_message) {
01377            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01378               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01379               break;
01380            } else {
01381               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01382            }
01383         }
01384     }
01385 
01386     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_MKDIR_REPLY) {
01387        IIIMP_mkdir_reply *p = (IIIMP_mkdir_reply *)receive_message->v.file_operation_reply.value;
01388        *ns_errno = p->ns_errno;
01389        return p->ns_id;
01390     }
01391     return -1;
01392 }
01393 
01394 int 
01395 rmdir_ns(
01396     iml_desktop_t *desktop,
01397     int ns_id,
01398     char *path,
01399     int *ns_errno
01400 )
01401 {
01402     IIIMP_message *send_message;
01403     IIIMP_message *receive_message;
01404     IIIMP_rmdir *iiimp_rmdir;
01405     IIIMP_utf8string *iiimp_dirname;
01406     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01407     *ns_errno = 0;
01408 
01409     if (xims == NULL)
01410        return -1;
01411     iiimp_dirname = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01412                                          (strlen(path) + 1),
01413                                          (const IIIMP_card8 *)path);
01414     if (!iiimp_dirname) return -1;
01415 
01416     iiimp_rmdir = iiimp_rmdir_new(xims->get_iiimptrans()->get_data_s(),
01417                                   ns_id, iiimp_dirname);
01418 
01419     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01420                                        xims->get_im_id(),
01421                                        IIIMP_FILE_OPERATION_TYPE_RMDIR,
01422                                        iiimp_rmdir);
01423 
01424     if(!xims->send(send_message, true)) return -1;
01425 
01426     for (;;) {
01427        receive_message = xims->get_iiimptrans()->receive();
01428         if (receive_message) {
01429            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01430               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01431               break;
01432            } else {
01433               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01434            }
01435         }
01436     }
01437 
01438     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_RMDIR_REPLY) {
01439        IIIMP_rmdir_reply *p = (IIIMP_rmdir_reply *)receive_message->v.file_operation_reply.value;
01440        *ns_errno = p->ns_errno;
01441        return p->ns_id;
01442     }
01443     return -1;
01444 }
01445 
01446 int 
01447 symlink_ns(
01448     iml_desktop_t *desktop,
01449     int ns_id,
01450     char *dpath,
01451     char *spath,
01452     int *ns_errno
01453 )
01454 {
01455     IIIMP_message *send_message;
01456     IIIMP_message *receive_message;
01457     IIIMP_symlink *iiimp_symlink;
01458     IIIMP_utf8string *iiimp_dname;
01459     IIIMP_utf8string *iiimp_sname;
01460     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01461     *ns_errno = 0;
01462 
01463     if (xims == NULL)
01464        return -1;
01465     iiimp_dname = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01466                                        (strlen(dpath) + 1),
01467                                    (const IIIMP_card8 *)dpath);
01468                                
01469     if (!iiimp_dname) return -1;
01470 
01471     iiimp_sname = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01472                                        (strlen(spath) + 1),
01473                                        (const IIIMP_card8 *)spath);
01474                                    
01475     if (!iiimp_sname) return -1;
01476 
01477     iiimp_symlink = iiimp_symlink_new(xims->get_iiimptrans()->get_data_s(),
01478                                       ns_id, iiimp_dname, iiimp_sname);
01479 
01480     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01481                                        xims->get_im_id(),
01482                                        IIIMP_FILE_OPERATION_TYPE_SYMLINK,
01483                                        iiimp_symlink);
01484 
01485     if(!xims->send(send_message, true)) return -1;
01486 
01487     for (;;) {
01488        receive_message = xims->get_iiimptrans()->receive();
01489         if (receive_message) {
01490            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01491               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01492               break;
01493            } else {
01494               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01495            }
01496         }
01497     }
01498 
01499     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_SYMLINK_REPLY) {
01500        IIIMP_symlink_reply *p = (IIIMP_symlink_reply *)receive_message->v.file_operation_reply.value;
01501        *ns_errno = p->ns_errno;
01502        return p->ns_id;
01503     }
01504     return -1;
01505 }
01506 
01507 int
01508 stat_ns(
01509     iml_desktop_t *desktop,
01510     int ns_id,
01511     char *path,
01512     struct stat *buf,
01513     int *ns_errno
01514 )
01515 {
01516     IIIMP_message *send_message;
01517     IIIMP_message *receive_message;
01518     IIIMP_stat *iiimp_stat;
01519     IIIMP_utf8string *iiimp_dirname;
01520     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01521     *ns_errno = 0;
01522 
01523     if (xims == NULL)
01524        return -1;
01525     iiimp_dirname = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01526                                          (strlen(path) + 1),
01527                                          (const IIIMP_card8 *)path);
01528     if (!iiimp_dirname) return -1;
01529 
01530     iiimp_stat = iiimp_stat_new(xims->get_iiimptrans()->get_data_s(),
01531                             ns_id, iiimp_dirname);
01532 
01533     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01534                                        xims->get_im_id(),
01535                                        IIIMP_FILE_OPERATION_TYPE_STAT,
01536                                        iiimp_stat);
01537 
01538     if(!xims->send(send_message, true)) return -1;
01539 
01540     for (;;) {
01541         receive_message = xims->get_iiimptrans()->receive();
01542         if (receive_message) {
01543            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01544               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !! \n");
01545               break;
01546            } else {
01547               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01548            }
01549         }
01550     }
01551 
01552     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_STAT_REPLY) {
01553        IIIMP_stat_reply *p = (IIIMP_stat_reply *)receive_message->v.file_operation_reply.value;
01554        if (p->ns_id < 0) {
01555            return p->ns_id;
01556        }
01557 
01558        (*buf).st_mode = p->stat_buf->s_mode;
01559        (*buf).st_nlink = p->stat_buf->s_nlink;
01560        (*buf).st_uid = p->stat_buf->s_uid;
01561        (*buf).st_gid = p->stat_buf->s_gid;
01562        (*buf).st_atime = p->stat_buf->s_atime;
01563        (*buf).st_mtime = p->stat_buf->s_mtime;
01564        (*buf).st_ctime = p->stat_buf->s_ctime;
01565        (*buf).st_blksize = p->stat_buf->s_blksize;
01566        (*buf).st_dev = p->stat_buf->s_dev;
01567        (*buf).st_rdev = p->stat_buf->s_rdev;
01568        (*buf).st_ino = p->stat_buf->s_ino;
01569        (*buf).st_size = p->stat_buf->s_size;
01570        (*buf).st_blocks = p->stat_buf->s_blocks;
01571 
01572        *ns_errno =  p->ns_errno;
01573        return p->ns_id;
01574     }
01575 
01576     return -1;
01577 }
01578 
01579 int
01580 lstat_ns(
01581     iml_desktop_t *desktop,
01582     int ns_id,
01583     char *path,
01584     struct stat *buf,
01585     int *ns_errno
01586 )
01587 {
01588     IIIMP_message *send_message;
01589     IIIMP_message *receive_message;
01590     IIIMP_lstat *iiimp_lstat;
01591     IIIMP_utf8string *iiimp_dirname;
01592     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01593     *ns_errno = 0;
01594 
01595     if (xims == NULL)
01596        return -1;
01597     iiimp_dirname = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01598                                          (strlen(path) + 1),
01599                                          (const IIIMP_card8 *)path);
01600     if (!iiimp_dirname) return -1;
01601 
01602     iiimp_lstat = iiimp_lstat_new(xims->get_iiimptrans()->get_data_s(),
01603                               ns_id, iiimp_dirname);
01604 
01605     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01606                                        xims->get_im_id(),
01607                                        IIIMP_FILE_OPERATION_TYPE_LSTAT,
01608                                        iiimp_lstat);
01609 
01610     if(!xims->send(send_message, true)) return -1;
01611 
01612     for (;;) {
01613         receive_message = xims->get_iiimptrans()->receive();
01614         if (receive_message) {
01615            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01616               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01617               break;
01618            } else {
01619               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01620            }
01621         }
01622     }
01623 
01624     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_LSTAT_REPLY) {
01625        IIIMP_lstat_reply *p = (IIIMP_lstat_reply *)receive_message->v.file_operation_reply.value;
01626        if (p->ns_id < 0) {
01627            return p->ns_id;
01628        }
01629 
01630        (*buf).st_mode = p->stat_buf->s_mode;
01631        (*buf).st_nlink = p->stat_buf->s_nlink;
01632        (*buf).st_uid = p->stat_buf->s_uid;
01633        (*buf).st_gid = p->stat_buf->s_gid;
01634        (*buf).st_atime = p->stat_buf->s_atime;
01635        (*buf).st_mtime = p->stat_buf->s_mtime;
01636        (*buf).st_ctime = p->stat_buf->s_ctime;
01637        (*buf).st_blksize = p->stat_buf->s_blksize;
01638        (*buf).st_dev = p->stat_buf->s_dev;
01639        (*buf).st_rdev = p->stat_buf->s_rdev;
01640        (*buf).st_ino = p->stat_buf->s_ino;
01641        (*buf).st_size = p->stat_buf->s_size;
01642        (*buf).st_blocks = p->stat_buf->s_blocks;
01643 
01644        *ns_errno =  p->ns_errno;
01645        return p->ns_id;
01646     }
01647 
01648     return -1;
01649 }
01650 
01651 int
01652 fstat_ns(
01653     iml_desktop_t *desktop,
01654     int ns_id,
01655     struct stat *buf,
01656     int *ns_errno
01657 )
01658 {
01659     IIIMP_message *send_message;
01660     IIIMP_message *receive_message;
01661     IIIMP_fstat *iiimp_fstat;
01662     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01663     *ns_errno = 0;
01664 
01665     if (xims == NULL)
01666        return -1;
01667     iiimp_fstat = iiimp_fstat_new(xims->get_iiimptrans()->get_data_s(),
01668                               ns_id);
01669 
01670     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01671                                        xims->get_im_id(),
01672                                        IIIMP_FILE_OPERATION_TYPE_FSTAT,
01673                                        iiimp_fstat);
01674     if(!xims->send(send_message, true)) return -1;
01675 
01676     for (;;) {
01677         receive_message = xims->get_iiimptrans()->receive();
01678         if (receive_message) {
01679            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01680               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01681               break;
01682            } else {
01683               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01684            }
01685         }
01686     }
01687 
01688     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_FSTAT_REPLY) {
01689        IIIMP_fstat_reply *p = (IIIMP_fstat_reply *)receive_message->v.file_operation_reply.value;
01690        if (p->ns_id < 0) {
01691            return p->ns_id;
01692        }
01693 
01694        (*buf).st_mode = p->stat_buf->s_mode;
01695        (*buf).st_nlink = p->stat_buf->s_nlink;
01696        (*buf).st_uid = p->stat_buf->s_uid;
01697        (*buf).st_gid = p->stat_buf->s_gid;
01698        (*buf).st_atime = p->stat_buf->s_atime;
01699        (*buf).st_mtime = p->stat_buf->s_mtime;
01700        (*buf).st_ctime = p->stat_buf->s_ctime;
01701        (*buf).st_blksize = p->stat_buf->s_blksize;
01702        (*buf).st_dev = p->stat_buf->s_dev;
01703        (*buf).st_rdev = p->stat_buf->s_rdev;
01704        (*buf).st_ino = p->stat_buf->s_ino;
01705        (*buf).st_size = p->stat_buf->s_size;
01706        (*buf).st_blocks = p->stat_buf->s_blocks;
01707 
01708        *ns_errno =  p->ns_errno;
01709        return p->ns_id;
01710     }
01711 
01712     return -1;
01713 }
01714 
01715 int   
01716 ftruncate_ns(
01717     iml_desktop_t *desktop,
01718     int ns_id, 
01719     off_t length,
01720     int *ns_errno
01721 )           
01722 {               
01723     IIIMP_message *send_message;
01724     IIIMP_message *receive_message;
01725     IIIMP_ftruncate *iiimp_ftruncate;
01726     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01727     *ns_errno = 0;
01728     
01729     if (xims == NULL)
01730        return -1;
01731     iiimp_ftruncate = iiimp_ftruncate_new(xims->get_iiimptrans()->get_data_s(),    
01732                                      ns_id, length);
01733 
01734     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01735                                        xims->get_im_id(),
01736                                        IIIMP_FILE_OPERATION_TYPE_FTRUNCATE,
01737                                        iiimp_ftruncate);
01738 
01739     if(!xims->send(send_message, true)) return -1;
01740     
01741     for (;;) {
01742         receive_message = xims->get_iiimptrans()->receive();
01743         if (receive_message) {
01744             if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01745                 LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01746                 break;
01747             } else {
01748                 xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01749             }
01750         }
01751     }
01752 
01753     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_FTRUNCATE_REPLY) {
01754        IIIMP_ftruncate_reply *p = (IIIMP_ftruncate_reply *)receive_message->v.file_operation_reply.value;
01755        *ns_errno = p->ns_errno;
01756        return p->ns_id;
01757     }
01758     return -1;
01759 }
01760 
01761 int 
01762 creat_ns(
01763     iml_desktop_t *desktop,
01764     int ns_id,
01765     char *path,
01766     mode_t mode,
01767     int *ns_errno
01768 )
01769 {
01770     IIIMP_message *send_message;
01771     IIIMP_message *receive_message;
01772     IIIMP_creat *iiimp_creat;
01773     IIIMP_utf8string *iiimp_fname;
01774     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01775     *ns_errno = 0;
01776 
01777     if (xims == NULL)
01778        return -1;
01779     iiimp_fname = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01780                                        (strlen(path) + 1),
01781                                    (const IIIMP_card8 *)path);
01782     if (!iiimp_fname) return -1;
01783 
01784     iiimp_creat = iiimp_creat_new(xims->get_iiimptrans()->get_data_s(),
01785                               ns_id, mode, iiimp_fname);
01786 
01787     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01788                                        xims->get_im_id(),
01789                                        IIIMP_FILE_OPERATION_TYPE_CREAT,
01790                                        iiimp_creat);
01791     if(!xims->send(send_message, true)) return -1;
01792 
01793     for (;;) {
01794        receive_message = xims->get_iiimptrans()->receive();
01795         if (receive_message) {
01796            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01797               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01798               break;
01799            } else {
01800               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01801            }
01802         }
01803     }
01804 
01805     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_CREAT_REPLY) {
01806        IIIMP_creat_reply *p = (IIIMP_creat_reply *)receive_message->v.file_operation_reply.value;
01807        *ns_errno = p->ns_errno;
01808        return p->ns_id;
01809     }
01810     return -1;
01811 }
01812 
01813 off_t 
01814 lseek_ns(
01815     iml_desktop_t *desktop,
01816     int ns_id,
01817     off_t offset,
01818     int whence,
01819     int *ns_errno
01820 )
01821 {
01822     IIIMP_message *send_message;
01823     IIIMP_message *receive_message;
01824     IIIMP_lseek *iiimp_lseek;
01825     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01826     *ns_errno = 0;
01827 
01828     if (xims == NULL)
01829        return -1;
01830     iiimp_lseek = iiimp_lseek_new(xims->get_iiimptrans()->get_data_s(),
01831                               ns_id, offset, whence);
01832 
01833     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01834                                        xims->get_im_id(),
01835                                        IIIMP_FILE_OPERATION_TYPE_LSEEK,
01836                                        iiimp_lseek);
01837 
01838     if(!xims->send(send_message, true)) return -1;
01839 
01840     for (;;) {
01841        receive_message = xims->get_iiimptrans()->receive();
01842         if (receive_message) {
01843            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01844               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01845               break;
01846            } else {
01847               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01848            }
01849         }
01850     }
01851 
01852     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_LSEEK) {
01853        IIIMP_lseek_reply *p = (IIIMP_lseek_reply *)receive_message->v.file_operation_reply.value;
01854        *ns_errno = p->ns_errno;
01855        return p->offset;
01856     }
01857     return -1;
01858 }
01859 
01860 int 
01861 unlink_ns(
01862     iml_desktop_t *desktop,
01863     int ns_id,
01864     char *path,
01865     int *ns_errno
01866 )
01867 {
01868     IIIMP_message *send_message;
01869     IIIMP_message *receive_message;
01870     IIIMP_unlink *iiimp_unlink;
01871     IIIMP_utf8string *iiimp_path;
01872     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01873     *ns_errno = 0;
01874 
01875     if (xims == NULL)
01876        return -1;
01877     iiimp_path = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01878                                       (strlen(path) + 1),
01879                                   (const IIIMP_card8 *)path);
01880     if (!iiimp_path) return -1;
01881 
01882     iiimp_unlink = iiimp_unlink_new(xims->get_iiimptrans()->get_data_s(),
01883                                 ns_id, iiimp_path);
01884 
01885     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01886                                        xims->get_im_id(),
01887                                        IIIMP_FILE_OPERATION_TYPE_UNLINK,
01888                                        iiimp_unlink);
01889 
01890     if(!xims->send(send_message, true)) return -1;
01891 
01892     for (;;) {
01893        receive_message = xims->get_iiimptrans()->receive();
01894         if (receive_message) {
01895            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01896               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01897               break;
01898            } else {
01899               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01900            }
01901         }
01902     }
01903 
01904     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_UNLINK_REPLY) {
01905        IIIMP_unlink_reply *p = (IIIMP_unlink_reply *)receive_message->v.file_operation_reply.value;
01906        *ns_errno = p->ns_errno;
01907        return p->ns_id;
01908     }
01909     return -1;
01910 }
01911 
01912 int 
01913 rename_ns(
01914     iml_desktop_t *desktop,
01915     int ns_id,
01916     char *old_name,
01917     char *new_name,
01918     int *ns_errno
01919 )
01920 {
01921     IIIMP_message *send_message;
01922     IIIMP_message *receive_message;
01923     IIIMP_rename *iiimp_rename;
01924     IIIMP_utf8string *iiimp_old_name;
01925     IIIMP_utf8string *iiimp_new_name;
01926     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01927     *ns_errno = 0;
01928 
01929     if (xims == NULL)
01930        return -1;
01931     iiimp_old_name = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01932                                           (strlen(old_name) + 1),
01933                                       (const IIIMP_card8 *)old_name);
01934     if (!iiimp_old_name) return -1;
01935 
01936     iiimp_new_name = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
01937                                           (strlen(new_name) + 1),
01938                                       (const IIIMP_card8 *)new_name);
01939     if (!iiimp_new_name) return -1;
01940 
01941     iiimp_rename = iiimp_rename_new(xims->get_iiimptrans()->get_data_s(),
01942                                 ns_id, 
01943                                 iiimp_old_name, 
01944                                 iiimp_new_name);
01945 
01946     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01947                                        xims->get_im_id(),
01948                                        IIIMP_FILE_OPERATION_TYPE_RENAME,
01949                                        iiimp_rename);
01950 
01951     if(!xims->send(send_message, true)) return -1;
01952 
01953     for (;;) {
01954        receive_message = xims->get_iiimptrans()->receive();
01955         if (receive_message) {
01956            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
01957               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
01958               break;
01959            } else {
01960               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
01961            }
01962         }
01963     }
01964 
01965     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_RENAME_REPLY) {
01966        IIIMP_rename_reply *p = (IIIMP_rename_reply *)receive_message->v.file_operation_reply.value;
01967        *ns_errno = p->ns_errno;
01968        return p->ns_id;
01969     }
01970     return -1;
01971 }
01972 
01973 int 
01974 fcntl_ns(
01975     iml_desktop_t *desktop,
01976     int ns_id,
01977     int cmd,
01978     int arg,
01979     int *ns_errno
01980 )
01981 {
01982     IIIMP_message *send_message;
01983     IIIMP_message *receive_message;
01984     IIIMP_fcntl *iiimp_fcntl;
01985     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
01986     *ns_errno = 0;
01987 
01988     if (xims == NULL)
01989        return -1;
01990     iiimp_fcntl = iiimp_fcntl_new(xims->get_iiimptrans()->get_data_s(),
01991                               ns_id, cmd, arg);
01992 
01993     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
01994                                        xims->get_im_id(),
01995                                        IIIMP_FILE_OPERATION_TYPE_FCNTL,
01996                                        iiimp_fcntl);
01997 
01998     if(!xims->send(send_message, true)) return -1;
01999 
02000     for (;;) {
02001        receive_message = xims->get_iiimptrans()->receive();
02002         if (receive_message) {
02003            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
02004               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
02005               break;
02006            } else {
02007               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
02008            }
02009         }
02010     }
02011 
02012     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_FCNTL_REPLY) {
02013        IIIMP_fcntl_reply *p = (IIIMP_fcntl_reply *)receive_message->v.file_operation_reply.value;
02014        *ns_errno = p->ns_errno;
02015        return p->return_value;
02016     }
02017     return -1;
02018 }
02019 
02020 int 
02021 truncate_ns(
02022     iml_desktop_t *desktop,
02023     int ns_id,
02024     char *path,
02025     off_t length,
02026     int *ns_errno
02027 )
02028 {
02029     IIIMP_message *send_message;
02030     IIIMP_message *receive_message;
02031     IIIMP_truncate *iiimp_truncate;
02032     IIIMP_utf8string *iiimp_path;
02033     IIIMP_IMState *xims = IIIMP_IMState::get_imstate_from_desktop(desktop);
02034     *ns_errno = 0;
02035 
02036     if (xims == NULL)
02037        return -1;
02038     iiimp_path = iiimp_utf8string_new(xims->get_iiimptrans()->get_data_s(),
02039                                       (strlen(path) + 1),
02040                                   (const IIIMP_card8 *)path);
02041     if (!iiimp_path) return -1;
02042 
02043     iiimp_truncate = iiimp_truncate_new(xims->get_iiimptrans()->get_data_s(),
02044                                    ns_id, iiimp_path, length);
02045 
02046     send_message = iiimp_file_operation_new(xims->get_iiimptrans()->get_data_s(),
02047                                        xims->get_im_id(),
02048                                        IIIMP_FILE_OPERATION_TYPE_TRUNCATE,
02049                                        iiimp_truncate);
02050 
02051     if(!xims->send(send_message, true)) return -1;
02052 
02053     for (;;) {
02054        receive_message = xims->get_iiimptrans()->receive();
02055         if (receive_message) {
02056            if (receive_message->opcode == IM_FILE_OPERATION_REPLY) {
02057               LOG_DEBUG("Got IM_FILE_OPERATION_REPLY !!\n");
02058               break;
02059            } else {
02060               xims->dispatch(receive_message->im_id, receive_message->ic_id, receive_message);
02061            }
02062         }
02063     }
02064 
02065     if (receive_message->v.file_operation_reply.type == IIIMP_FILE_OPERATION_TYPE_TRUNCATE_REPLY) {
02066        IIIMP_truncate_reply *p = (IIIMP_truncate_reply *)receive_message->v.file_operation_reply.value;
02067        *ns_errno = p->ns_errno;
02068        return p->ns_id;
02069     }
02070     return -1;
02071 }
02072 
02073 /* Local Variables: */
02074 /* c-file-style: "iiim-project" */
02075 /* End: */