Changeset c350322462ba2718dfb41b7b9de6eac9b20a22b4

Show
Ignore:
Timestamp:
09/09/09 04:35:30 (4 years ago)
Author:
Benjamin Herr <ben@…>
Parents:
46525d829153a9ecac4e3904e3c4b0e80a473bcc
Children:
724203b9b50a9c0d08582a3f2b632fbd7d553369
git-committer:
Benjamin Herr <ben@0x539.de> / 2009-09-09T04:35:30Z+0200
Message:

main.cpp: moved most WITH_UNIQUE code out of main()

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • code/main.cpp

    r46525d8 rc350322  
    8080#endif 
    8181        } 
     82 
     83#ifdef WITH_UNIQUE 
     84        int send_message_with_uris(UniqueApp* app, 
     85                                    gint message_id, 
     86                                    const std::vector<Glib::ustring>& uris) 
     87        { 
     88                std::vector<const gchar*> uri_cstrs(uris.size() + 1); 
     89                for(int i = 0; i < uris.size(); ++i) 
     90                        uri_cstrs[i] = uris[i].c_str(); 
     91 
     92                UniqueMessageData* message = unique_message_data_new(); 
     93                unique_message_data_set_uris( 
     94                        message, const_cast<gchar**>(&uri_cstrs[0])); 
     95                UniqueResponse response = unique_app_send_message( 
     96                        app, message_id, message); 
     97                unique_message_data_free(message); 
     98 
     99                if(response == UNIQUE_RESPONSE_OK) 
     100                { 
     101                        return 0; 
     102                } 
     103                else 
     104                { 
     105                        std::cerr 
     106                                << "error sending URIs to existing gobby " 
     107                                   "instance (libunique): " 
     108                                << static_cast<int>(response) 
     109                                << std::endl; 
     110                        return -1; 
     111                } 
     112        } 
     113 
     114        int my_unique_activate(UniqueApp* app) { 
     115                UniqueResponse response = 
     116                        unique_app_send_message(app, UNIQUE_ACTIVATE, NULL); 
     117                if(response != UNIQUE_RESPONSE_OK) 
     118                { 
     119                        std::cerr 
     120                                << "error activating existing gobby " 
     121                                   "instance (libunique): " 
     122                                << static_cast<int>(response) 
     123                                << std::endl; 
     124                        return -1; 
     125                } 
     126        } 
     127 
     128        int my_unique_send_file_args(UniqueApp* app, 
     129                                     int argc, 
     130                                     const char* const* argv) 
     131        { 
     132                std::vector<Glib::ustring> uris(argc); 
     133                for(int i = 0; i < argc; ++i) 
     134                { 
     135                        uris[i] = Gio::File::create_for_commandline_arg( 
     136                                        argv[i])->get_uri(); 
     137                } 
     138                return send_message_with_uris(app, UNIQUE_OPEN, uris); 
     139        } 
     140 
     141        int my_unique_check_other(UniqueApp* app, 
     142                                  int argc, const char* const* argv) 
     143        { 
     144                if(argc == 0) 
     145                { 
     146                        return my_unique_activate(app); 
     147                } 
     148                else 
     149                { 
     150                        if(my_unique_send_file_args(app, argc, argv) != 0) 
     151                                return -1; 
     152                } 
     153 
     154                return 0; 
     155        } 
     156#endif // WITH_UNIQUE 
    82157} 
    83158 
     
    152227        UniqueApp* app = unique_app_new("de._0x539.gobby", NULL); 
    153228 
    154         if (!new_instance && unique_app_is_running(app)) 
    155         { 
    156                 // TODO: Also send the hostnames to connect to to the remote 
    157                 // Gobby. 
    158                 UniqueResponse response; 
    159                 if (argc < 2) 
    160                 { 
    161                         response = unique_app_send_message( 
    162                                 app, UNIQUE_ACTIVATE, NULL); 
    163                 } 
    164                 else 
    165                 { 
    166                         UniqueMessageData* message = 
    167                                 unique_message_data_new(); 
    168                         std::vector<const gchar*> uris(argc); 
    169                         std::vector<Glib::ustring> uri_strs(argc-1); 
    170                         for (int i = 0; i < argc - 1; ++i) { 
    171                                 uri_strs[i] = 
    172                                         Gio::File::create_for_commandline_arg( 
    173                                                 argv[i+1])->get_uri(); 
    174                                 uris[i] = uri_strs[i].c_str(); 
    175                         } 
    176  
    177                         unique_message_data_set_uris( 
    178                                 message, const_cast<gchar**>(&uris[0])); 
    179                         response = unique_app_send_message( 
    180                                 app, UNIQUE_OPEN, message); 
    181                         unique_message_data_free(message); 
    182                 } 
    183  
     229        if(!new_instance && unique_app_is_running(app)) 
     230        { 
     231                int exit_code = my_unique_check_other(app, argc - 1, argv + 1); 
    184232                g_object_unref(app); 
    185  
    186                 if (response == UNIQUE_RESPONSE_OK) 
    187                 { 
    188                         return 0; 
    189                 } 
    190                 else  
    191                 { 
    192                         std::cerr 
    193                                 << "uniqueapp error: " 
    194                                 << static_cast<int>(response) 
    195                                 << std::endl; 
    196                         return -1; 
    197                 } 
     233                return exit_code; 
    198234        } 
    199235#endif // WITH_UNIQUE