Files
textmate/Frameworks/network/src/user_agent.cc
Allan Odgaard 398e55cdbe Add helper functions to get OS version
We need this in a few places and while calling Gestalt() isn’t that much code, that function is deprecated in 10.8 and the alternative is a lot more code, so we don’t want to repeat that once we update the code.
2013-03-17 15:44:06 +01:00

54 lines
1.4 KiB
C++

#include "user_agent.h"
#include <OakSystem/application.h>
#include <text/format.h>
#include <plist/plist.h>
#include <io/path.h>
#include <cf/cf.h>
#include <oak/compat.h>
static std::string hardware_info (int field, bool integer = false)
{
char buf[1024];
size_t bufSize = sizeof(buf);
int request[] = { CTL_HW, field };
if(sysctl(request, sizeofA(request), buf, &bufSize, NULL, 0) != -1)
{
if(integer && bufSize == 4)
return std::to_string(*(int*)buf);
return std::string(buf, buf + bufSize - 1);
}
return "???";
}
static std::string user_uuid ()
{
std::string res = NULL_STR;
if(CFStringRef str = (CFStringRef)CFPreferencesCopyAppValue(CFSTR("SoftwareUpdateUUID"), kCFPreferencesCurrentApplication))
{
if(CFGetTypeID(str) == CFStringGetTypeID())
res = cf::to_s(str);
CFRelease(str);
}
if(res == NULL_STR)
{
res = oak::uuid_t().generate();
CFPreferencesSetAppValue(CFSTR("SoftwareUpdateUUID"), cf::wrap(res), kCFPreferencesCurrentApplication);
CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
}
return res;
}
std::string create_agent_info_string ()
{
return text::format("%s/%s/%s %zu.%zu.%zu/%s/%s/%s", oak::application_t::name().c_str(), oak::application_t::revision().c_str(), user_uuid().c_str(),
oak::os_major(), oak::os_minor(), oak::os_patch(),
hardware_info(HW_MACHINE).c_str(),
hardware_info(HW_MODEL).c_str(),
hardware_info(HW_NCPU, true).c_str()
);
}