Back to index

im-sdk  12.3.91
IMDesktop.cpp
Go to the documentation of this file.
00001 #include <config.h>
00002 #include <stdio.h>
00003 #include <signal.h>
00004 #include "IMDesktop.hh"
00005 #include "IMUser.hh"
00006 #include "IMInputContext.hh"
00007 #include "IMConnection.hh"
00008 #include "LE.hh"
00009 #include "IMLog.hh"
00010 #include <SunIM.h>
00011 
00012 const char unknown_display_name[] = "Unknown:0";
00013 
00014 void
00015 IMDesktop::initialize()
00016 {
00017     IMDesktopAttr::iterator it;
00018 
00019     iiim_xbe = false;
00020 
00021     it = attrs.find(CLIENT_NAME);
00022     if (it == attrs.end()) {
00023        client_type = UNKNOWN_CLIENT;
00024     } else {
00025        u16string& client_name = it->second;
00026        if (client_name.find("Htt XIM") != u16string::npos) {
00027            iiim_xbe = true;
00028            client_type = IIIMXCF;
00029        } else if (client_name.find("XBE XIM") != u16string::npos) {
00030            iiim_xbe = true;
00031            client_type = IIIMXCF;
00032        } else if (client_name.find("XIM IIIMP") != u16string::npos) {
00033            client_type = IIIMXCF;
00034        } else if (client_name.find("JDK") != u16string::npos) {
00035            client_type = IIIMJCF;
00036        } else if (client_name.find("GTK IIIMCF Module") != u16string::npos) {
00037            client_type = IIIMGCF;
00038        } else if (client_name.find("IIIMECF") != u16string::npos) {
00039            client_type = IIIMECF;
00040        }
00041     }
00042 
00043     // set alternative display_name.
00044     const u16string* pstr = get_attr(X_DISPLAY_NAME);
00045     if (pstr && pstr->get_charstr()) {
00046        desktop_display_name = pstr->get_charstr();
00047     } else {
00048        pstr = get_attr(CLIENT_HOST);
00049        if (!pstr) {
00050            LOG_ERROR("IMDesktop is requested, but the client host is unknown.");
00051        }
00052        if (pstr->get_charstr()) {
00053            desktop_display_name = pstr->get_charstr();
00054        } else {
00055            desktop_display_name = "unknown";
00056        }
00057        desktop_display_name.append(":0");
00058     }
00059 }
00060 
00061 iml_desktop_t*
00062 IMDesktop::create_iml_desktop(
00063     LEContext &lec
00064 )
00065 {
00066     iml_desktop_t *pidt;
00067     const char *username, *hostname, *displayname;
00068     IMDesktopAttr::iterator it;
00069     IMConnection *pimc = lec.get_inputcontext()->get_imconnection();
00070 
00071     username = puser->get_username().get_charstr();
00072     if (!username) username = "";
00073     hostname = pimc->get_hostname().c_str();
00074 
00075     it = attrs.find(X_DISPLAY_NAME);
00076     if (it != attrs.end()) {
00077        displayname = it->second.get_charstr();
00078        if (!displayname)
00079            displayname = unknown_display_name;
00080     } else {
00081        displayname = unknown_display_name;
00082     }
00083     pidt = new_user(lec.get_lebase()->get_iml_if(),
00084                   username, hostname, displayname);
00085 
00086     return pidt;
00087 }
00088 
00089 iml_desktop_t*
00090 IMDesktop::request_iml_desktop(
00091     LEContext &lec
00092 )
00093 {
00094     iml_desktop_t *pidt;
00095     LEBase *plebase = lec.get_lebase();
00096     IMLDesktopMap::iterator it = iml_desktop_map.find(plebase);
00097     if (it == iml_desktop_map.end()) {
00098        pidt = create_iml_desktop(lec);
00099        if (!pidt) return NULL;
00100        if (!plebase->open_iml_desktop(this, pidt)) {
00101            del_user(pidt);
00102            return NULL;
00103        }
00104        iml_desktop_map[plebase] = pidt;
00105     } else {
00106        pidt = it->second;
00107     }
00108 
00109     return pidt;
00110 }
00111 
00112 void
00113 IMDesktop::release_iml_desktop(
00114     LEContext &lec,
00115     iml_desktop_t *iml_desktop
00116 )
00117 {
00118     // TODO: need to pass command line option -desktop.
00119     // Use environment varialbe tentatively for now.
00120     static int iiimd_option_desktop;
00121     if (0 == iiimd_option_desktop) {
00122        if (NULL != getenv("IIIMD_OPTION_DESKTOP")) {
00123            iiimd_option_desktop = 1;
00124        } else {
00125            iiimd_option_desktop = -1;
00126        }
00127     }
00128 
00129     // TODO: Life-cycles of iml_desktop should be controlled by LEBase.
00130     // But it would involve redesign of LEbase.  Currently, the life-cycle
00131     // is reference of session.
00132     if (!iml_desktop->session_list) {
00133        LEBase *plebase = lec.get_lebase();
00134        IMLDesktopMap::iterator it = iml_desktop_map.find(plebase);
00135        ASSERT(it != iml_desktop_map.end());
00136        ASSERT(it->second == iml_desktop);
00137        plebase->close_iml_desktop(iml_desktop);
00138        del_user(iml_desktop);
00139        iml_desktop_map.erase(it);
00140        if ((1 == iiimd_option_desktop) && (0 == iml_desktop_map.size())) {
00141            kill(getpid(), SIGTERM);
00142        }
00143     }
00144 }
00145 
00146 bool
00147 IMDesktop::match(
00148     IMDesktop::IMDesktopAttr &req_attrs
00149 )
00150 {
00151     IMDesktopAttr::iterator itr;
00152     IMDesktopAttr::iterator it;
00153 
00154     for (itr = req_attrs.begin(); itr != req_attrs.end(); itr++) {
00155        // need to compare client host and display names only
00156        if (itr->first == IMDesktop::CLIENT_HOST ||
00157            itr->first == IMDesktop::X_DISPLAY_NAME) {
00158          it = attrs.find(itr->first);
00159          if (it == attrs.end()) return false;
00160          if (it->second.compare(itr->second) != 0) return false;
00161        }
00162     }
00163     return true;
00164 }
00165 
00166 const u16string*
00167 IMDesktop::get_attr(
00168     enum IMDESKTOP_ATTRS attr
00169 )
00170 {
00171     IMDesktopAttr::iterator it = attrs.find(attr);
00172     if (it == attrs.end()) return NULL;
00173     return &it->second;
00174 }
00175 
00176 void
00177 IMDesktop::get_iml_desktop_args(
00178     vector<IMArg> &args
00179 )
00180 {
00181     IMArg a;
00182 
00183     args.reserve(MAX_UI_ATTRIBUTE);
00184 
00185     const u16string *pstr;
00186 
00187     a.id = UI_USER_NAME;
00188     a.value = (char*) puser->get_username().get_charstr();
00189     if (!a.value) a.value = "Nobody";
00190     args.push_back(a);
00191 
00192     a.id = UI_HOST_NAME;
00193     pstr = get_attr(IMDesktop::CLIENT_HOST);
00194     if (pstr && pstr->get_charstr()) {
00195        a.value = (char*) pstr->get_charstr();
00196     } else {
00197        LOG_DEBUG("client host name is unknown.  It must be a problem.");
00198        a.value = "unknown";
00199     }
00200     args.push_back(a);
00201 
00202     a.id = UI_DISPLAY_ID;
00203     a.value = (char*) get_desktop_display_name().c_str();
00204     args.push_back(a);
00205 
00206     a.id = UI_PROTOCOL_TYPE;
00207     if (iiim_xbe_p())
00208        a.value = UI_PROTOCOL_TYPE_XIMP;
00209     else
00210        a.value = UI_PROTOCOL_TYPE_IIIMP;
00211     args.push_back(a);
00212 
00213     a.id = UI_CLIENT_TYPE;
00214     switch (get_client_type()) {
00215       case IMDesktop::IIIMXCF:
00216       case IMDesktop::IIIMGCF:
00217        a.value = UI_CLIENT_TYPE_X;
00218        break;
00219       case IMDesktop::IIIMJCF:
00220        a.value = UI_CLIENT_TYPE_JAVA;
00221        break;
00222       case IMDesktop::IIIMECF:
00223       case IMDesktop::UNKNOWN_CLIENT:
00224       default:
00225        a.value = UI_CLIENT_TYPE_UNKNOWN;
00226        break;
00227     }
00228     args.push_back(a);
00229 
00230     a.id = UI_XSERVER_VENDOR;
00231     pstr = get_attr(IMDesktop::X_SERVER_VENDOR);
00232     if (pstr && pstr->get_charstr()) {
00233        a.value = (char*) pstr->get_charstr();
00234     } else {
00235        a.value = "unknown";
00236     }
00237     args.push_back(a);
00238 
00239     a.id = UI_OS_NAME;
00240     pstr = get_attr(IMDesktop::OS_NAME);
00241     if (pstr && pstr->get_charstr()) {
00242        a.value = (char*) pstr->get_charstr();
00243     } else {
00244        a.value = "unknown";
00245     }
00246     args.push_back(a);
00247 
00248     a.id = UI_OS_ARCH;
00249     pstr = get_attr(IMDesktop::OS_ARCH);
00250     if (pstr && pstr->get_charstr()) {
00251        a.value = (char*) pstr->get_charstr();
00252     } else {
00253        a.value = "unknown";
00254     }
00255     args.push_back(a);
00256 
00257     a.id = UI_OS_VERSION;
00258     pstr = get_attr(IMDesktop::OS_VERSION);
00259     if (pstr && pstr->get_charstr()) {
00260        a.value = (char*) pstr->get_charstr();
00261     } else {
00262        a.value = "unknown";
00263     }
00264     args.push_back(a);
00265 
00266     a.id = UI_AUTH_PASSWD;
00267     // For security reason, currently
00268     // we don't pass a clear password to LE.
00269     a.value = NULL;
00270     args.push_back(a);
00271 
00272     return;
00273 }
00274 
00275 void
00276 IMDesktop::get_lecontext_args(
00277     vector<IMArg> &args
00278 )
00279 {
00280     IMArg a;
00281 
00282     //TODO!!!
00283     IMSetArg(a, SC_LOOKUP_LABELTYPE, "numeric");
00284     args.push_back(a);
00285     IMSetArg(a, SC_LOOKUP_NCOLS, 4);
00286     args.push_back(a);
00287     IMSetArg(a, SC_LOOKUP_NROWS, 4);
00288     args.push_back(a);
00289 
00290     return;
00291 }
00292 
00293 IMDesktop::IMDesktop(
00294     IMUser *pu
00295 )
00296 {
00297     puser = pu;
00298     initialize();
00299 }
00300 
00301 IMDesktop::IMDesktop(
00302     IMUser *pu,
00303     IMDesktopAttr &x_attrs
00304 )
00305 {
00306     puser = pu;
00307     attrs = x_attrs;
00308     initialize();
00309 }
00310 
00311 IMDesktop::~IMDesktop()
00312 {
00313     LEBase *plebase;
00314     iml_desktop_t *pidt;
00315     IMLDesktopMap::iterator it;
00316     for (it = iml_desktop_map.begin(); it != iml_desktop_map.end(); it++) {
00317        plebase = it->first;
00318        pidt = it->second;
00319        plebase->close_iml_desktop(pidt);
00320        del_user(pidt);
00321     }
00322 }
00323 
00324 /* Local Variables: */
00325 /* c-file-style: "iiim-project" */
00326 /* End: */